com.cognitect.transit.impl.ReadCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of transit-java Show documentation
Show all versions of transit-java Show documentation
Transit is a data format and a set of libraries for conveying values between applications written in different languages. This library provides support for marshalling Transit data to/from Java.
// Copyright (c) Cognitect, Inc.
// All rights reserved.
package com.cognitect.transit.impl;
public class ReadCache {
private Object[] cache;
private int index;
public ReadCache() {
cache = new Object[WriteCache.MAX_CACHE_ENTRIES];
index = 0;
}
private boolean cacheCode(String s) {
if((s.charAt(0) == Constants.SUB) && (!s.equals(Constants.MAP_AS_ARRAY)))
return true;
else
return false;
}
private int codeToIndex(String s) {
int sz = s.length();
if (sz == 2) {
return ((int)s.charAt(1) - WriteCache.BASE_CHAR_IDX);
} else {
return (((int)s.charAt(1) - WriteCache.BASE_CHAR_IDX) * WriteCache.CACHE_CODE_DIGITS) +
((int)s.charAt(2) - WriteCache.BASE_CHAR_IDX);
}
}
public Object cacheRead(String s, boolean asMapKey) { return cacheRead(s, asMapKey, null); }
public Object cacheRead(String s, boolean asMapKey, AbstractParser p) {
if(s.length() != 0) {
if(cacheCode(s)) {
return cache[codeToIndex(s)];
} else if(WriteCache.isCacheable(s, asMapKey)) {
if(index == WriteCache.MAX_CACHE_ENTRIES) {
init();
}
return cache[index++] = (p != null ? p.parseString(s) : s);
}
}
return p != null ? p.parseString(s) : s;
}
public ReadCache init(){
//need not clear array
index = 0;
return this;
}
}