A basic atomic number implementation

A basic atomic number implementation in python.

from datetime import datetime, date
from functools import wraps
import threading

def synchronized(function):

    def synched_function(self, *args, **kwargs):
        function._lock__ = threading.Lock()
        with function._lock__:
            return function(self, *args, **kwargs)
    return synched_function

class AtomicLong:
    def __init__(self, num):
        self._num = num

    def increment_and_get(self):
        self._num += 1
        return self._num

    def add_and_get(self, val):
        self._num += val
        return self._num

    def set_value(self, val):
        self._num = val

class IdGenerator:
    RESET_MARKER = 101

    def generate_id():
        # some business logic here
        sequence_number = IdGenerator.ATOMIC_LONG.increment_and_get()
        first_day = date(date.today().year, 1, 1)
        today = date.today()
        diff_days = (today - first_day).days
        year = date.today().year % 100
        seconds_passed_since_midnight = int(
            (datetime.now() - datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds())

        id = "{0}{1}{2}{3}{4}-{5}".format("A", "O", diff_days, year,
        if sequence_number > IdGenerator.MAX_SEQUENCE_VALUE:
        return id

if __name__ == '__main__':
    id_gen = IdGenerator()
    thread1 = threading.Thread(target=id_gen.generate_id)
    thread2 = threading.Thread(target=id_gen.generate_id)
    thread3 = threading.Thread(target=id_gen.generate_id)

Jackson JSON parser – Writing Custom Deserializer

One of major difference between Jackson and GSON is how they handle json parsing and mapping the json to a class object. While doing so Jackson uses strict checking which means the object from which the json was created and the class to which the json has to be mapped should be in strict sync.

Consider the following example –

class Foo {
private String value;
class Bar {
private String value;


String json = "{\"value\" : \"whatever\"}";
new Gson().fromJson(json, Foo.class);
new Gson().fromJson(json, Bar.class);

Gson is setup to perform a best effort to deserialize the given JSON into an instance of the given class. It will map as many fields as it finds. If none are found, that’s too bad, mapping will not be done.

Coming to the custom deserializer thing. We may need to write one in few cases as – changing date format of the json string to our custom one. For writing our custom deserializer we need to write a class which extends JsonDeserializer<T>. In this class we implement  the method deserialize(…) which contains our custom conversion code.

public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
public Date deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
String date = jsonParser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);

Now annotate the field in the POJO which has to be deserialized using the custom deserializer.

@JsonDeserialize(using = CustomJsonDateDeserializer.class) private Date createdAt;

That’s it. Pretty simple thing.

The Demigod Does It Again.

A day every Indian will be proud of. The indelible imprints which Tendulkar has set on our conscience will never get erased. What a stellar performance! Today also he proved that nothing can get
greater than him when it comes to cricket. What Tendulkar did was simply a divine magnanimous demonstration of majesty. Every moment he spent today in the ground, was so pulsating and
exhilirating that every body seemed dumbstruck. He was unstoppable. Calmly and placidly the intrepid Tendulkar was making everything look very easy, but the dexterity, was a sterling one. It seemed that a thousand of run was scored today.
The environment was simply “Tendulkarised”.
Even the Gods would have stopped writing futures of humanity,for watching him play and would have given 10-15 years of spare life to many, out of happiness. Ohhh, Dear God! Yet again, I think I’ve been stung by the deadly venom named “Tendulkar” and I’ll be happy if I die with it.
God bless Tendulkar.