public static int id(String token){
  long attempt_read_lock=lock.tryOptimisticRead();
  if (stringToId.containsKey(token)) {
    int resultId=stringToId.get(token);
    if (lock.validate(attempt_read_lock)) {
      return resultId;
    }
  }
  long read_lock_stamp=lock.readLock();
  try {
    if (stringToId.containsKey(token)) {
      return stringToId.get(token);
    }
  }
  finally {
    lock.unlockRead(read_lock_stamp);
  }
  long write_lock_stamp=lock.writeLock();
  try {
    if (stringToId.containsKey(token)) {
      return stringToId.get(token);
    }
    int id=idToString.size() * (FormatUtils.isNonterminal(token) ? -1 : 1);
    for (    NGramLanguageModel lm : LMs)     lm.registerWord(token,Math.abs(id));
    idToString.add(token);
    stringToId.put(token,id);
    return id;
  }
  finally {
    lock.unlockWrite(write_lock_stamp);
  }
}

public static int id ( String token ) { long attempt_read_lock = lock . tryOptimisticRead ( ) ; if ( stringToId . containsKey ( token ) ) { int resultId = stringToId . get ( token ) ; if ( lock . validate ( attempt_read_lock ) ) { return resultId ; } } long read_lock_stamp = lock . readLock ( ) ; try { if ( stringToId . containsKey ( token ) ) { return stringToId . get ( token ) ; } } finally { lock . unlockRead ( read_lock_stamp ) ; } long write_lock_stamp = lock . writeLock ( ) ; try { if ( stringToId . containsKey ( token ) ) { return stringToId . get ( token ) ; } int id = idToString . size ( ) * ( FormatUtils . isNonterminal ( token ) ? - 1 : 1 ) ; for ( NGramLanguageModel lm : LMs ) lm . registerWord ( token , Math . abs ( id ) ) ; idToString . add ( token ) ; stringToId . put ( token , id ) ; return id ; } finally { lock . unlockWrite ( write_lock_stamp ) ; } }
get the id of the token if it already exists , new id is created otherwise . todo : currently locks for every call . separate constant ( frozen ) ids from changing ( e . g . oov ) ids . constant ids could be immutable - & gt ; no locking . alternatively : could we use concurrenthashmap to not have to lock if actually contains it and only lock for modifications ?
