1 30 package org.jruby.runtime.marshal; 31 32 import java.io.IOException ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import org.jruby.RubySymbol; 37 import org.jruby.runtime.builtin.IRubyObject; 38 39 public class MarshalCache { 40 private Map linkCache = new HashMap (); 41 private Map symbolCache = new HashMap (); 42 43 public boolean isRegistered(IRubyObject value) { 44 return selectCache(value).containsKey(value); 45 } 46 47 public void register(IRubyObject value) { 48 Map cache = selectCache(value); 49 cache.put(value, new Integer (cache.size())); 50 } 51 52 private int registeredIndex(IRubyObject value) { 53 return ((Integer ) selectCache(value).get(value)).intValue(); 54 } 55 56 private Map selectCache(IRubyObject value) { 57 return (value instanceof RubySymbol) ? symbolCache : linkCache; 58 } 59 60 public void writeLink(MarshalStream output, IRubyObject value) throws IOException { 61 output.write(linkType(value)); 62 output.writeInt(registeredIndex(value)); 63 } 64 65 private static char linkType(IRubyObject value) { 66 return (value instanceof RubySymbol) ? ';' : '@'; 67 } 68 } 69 | Popular Tags |