001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object;
005:
006: import java.util.regex.Pattern;
007:
008: import gnu.trove.TObjectIntHashMap;
009: import gnu.trove.TObjectIntIterator;
010:
011: /**
012: * Some utility stuff for logical serialization
013: */
014: public class SerializationUtil {
015:
016: // NOTE: DO NOT USE VALUE 0. A zero indicates a mapping that does exist
017: public final static int ADD = 1;
018: public final static int ADD_AT = 2;
019: public final static int ADD_LAST = 3;
020: public final static int ADD_FIRST = 4;
021: public final static int PUT = 5;
022: public final static int CLEAR = 6;
023: public final static int REMOVE = 7;
024: public final static int REMOVE_AT = 8;
025: public final static int SET = 9;
026: public final static int SET_ELEMENT = 10;
027: public final static int SET_SIZE = 11;
028: public final static int TRIM_TO_SIZE = 12;
029: public final static int INSERT_AT = 13;
030: public final static int REMOVE_FIRST = 14;
031: public final static int REMOVE_LAST = 15;
032: public final static int REMOVE_RANGE = 16;
033: public final static int SET_TIME = 17;
034: public final static int SET_NANOS = 18;
035: public final static int REMOVE_ALL = 19;
036: public final static int VIEW_SET = 20;
037: public final static int GET = 21;
038: public final static int TAKE = 22;
039: public final static int REMOVE_FIRST_N = 23;
040: public final static int REPLACE = 24;
041: public final static int REPLACE_IF_VALUE_EQUAL = 25;
042: public final static int PUT_IF_ABSENT = 26;
043: public final static int REMOVE_IF_VALUE_EQUAL = 27;
044: public final static int URL_SET = 28;
045:
046: public final static String PUSH_SIGNATURE = "push(Ljava/lang/Object;)java/lang/Object;";
047: public final static String POP_SIGNATURE = "pop()java/lang/Object;";
048: public final static String ADD_AT_SIGNATURE = "add(ILjava/lang/Object;)V";
049: public final static String INSERT_ELEMENT_AT_SIGNATURE = "insertElementAt(Ljava/lang/Object;I)V";
050: public final static String ADD_ELEMENT_SIGNATURE = "addElement(Ljava/lang/Object;)V";
051: public final static String ADD_ALL_AT_SIGNATURE = "addAll(ILjava/util/Collection;)Z";
052: public final static String ADD_SIGNATURE = "add(Ljava/lang/Object;)Z";
053: public final static String ADD_ALL_SIGNATURE = "addAll(Ljava/util/Collection;)Z";
054: public final static String ADD_LAST_SIGNATURE = "addLast(Ljava/lang/Object;)V";
055: public final static String ADD_FIRST_SIGNATURE = "addFirst(Ljava/lang/Object;)V";
056: public final static String CLEAR_SIGNATURE = "clear()V";
057: public final static String PUT_SIGNATURE = "put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;";
058: public final static String GET_SIGNATURE = "get(Ljava/lang/Object;)Ljava/lang/Object;";
059: public final static String SET_SIGNATURE = "set(ILjava/lang/Object;)Ljava/lang/Object;";
060: public final static String SET_ELEMENT_SIGNATURE = "setElementAt(Ljava/lang/Object;I)V";
061: public final static String TRIM_TO_SIZE_SIGNATURE = "trimToSize()V";
062: public final static String SET_SIZE_SIGNATURE = "setSize(I)V";
063: public final static String REMOVE_AT_SIGNATURE = "remove(I)Ljava/lang/Object;";
064: public final static String REMOVE_SIGNATURE = "remove(Ljava/lang/Object;)Z";
065: public final static String REMOVE_KEY_SIGNATURE = "remove(Ljava/lang/Object;)Ljava/lang/Object;";
066: public final static String REMOVE_ENTRY_FOR_KEY_SIGNATURE = "removeEntryForKey(Ljava/lang/Object;)Ljava/util/HashMap$Entry;";
067: public final static String REMOVE_ELEMENT_SIGNATURE = "removeElement(Ljava/lang/Object;)Z";
068: public final static String REMOVE_ELEMENT_AT_SIGNATURE = "removeElementAt(I)V";
069: public final static String REMOVE_ALL_ELEMENTS_SIGNATURE = "removeAllElements()V";
070: public final static String REMOVE_ALL_SIGNATURE = "removeAll(Ljava/util/Collection;)Z";
071: public final static String ITERATOR_SIGNATURE = "iterator()Ljava/util/Iterator;";
072: public final static String TROVE_REMOVE_AT_SIGNATURE = "removeAt(I)V";
073: public final static String REMOVE_FIRST_SIGNATURE = "removeFirst()Ljava/lang/Object;";
074: public final static String REMOVE_LAST_SIGNATURE = "removeLast()Ljava/lang/Object;";
075: public final static String REMOVE_RANGE_SIGNATURE = "removeRange(II)V";
076: public final static String TO_ARRAY_SIGNATURE = "toArray([Ljava/lang/Object;)[Ljava/lang/Object;";
077: public final static String COPY_INTO_SIGNATURE = "copyInto([Ljava/lang/Object;)V";
078: public final static String SUBSET_SIGNATURE = "subSet(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/SortedSet;";
079: public final static String HEADSET_SIGNATURE = "headSet(Ljava/lang/Object;)Ljava/util/SortedSet;";
080: public final static String TAILSET_SIGNATURE = "tailSet(Ljava/lang/Object;)Ljava/util/SortedSet;";
081: public final static String EXECUTE_SIGNATURE = "execute(Ljava/lang/Object;)Z";
082: public final static String ENTRY_SET_SIGNATURE = "entrySet()Ljava/util/Set;";
083: public final static String KEY_SET_SIGNATURE = "keySet()Ljava/util/Set;";
084: public final static String VALUES_SIGNATURE = "values()Ljava/util/Collection;";
085: public final static String SET_TIME_SIGNATURE = "setTime(J)V";
086: public final static String SET_YEAR_SIGNATURE = "setYear(I)V";
087: public final static String SET_MONTH_SIGNATURE = "setMonth(I)V";
088: public final static String SET_DATE_SIGNATURE = "setDate(I)V";
089: public final static String SET_HOURS_SIGNATURE = "setHours(I)V";
090: public final static String SET_MINUTES_SIGNATURE = "setMinutes(I)V";
091: public final static String SET_SECONDS_SIGNATURE = "setSeconds(I)V";
092: public final static String SET_NANOS_SIGNATURE = "setNanos(I)V";
093: public final static String ITERATOR_REMOVE_SIGNATURE = "remove()V";
094: public final static String ELEMENTS_SIGNATURE = "elements()Ljava/util/Enumeration;";
095: public final static String QUEUE_PUT_SIGNATURE = "put(Ljava/lang/Object;)V";
096: public final static String OFFER_SIGNATURE = "offer(Ljava/lang/Object;)Z";
097: public final static String OFFER_TIMEOUT_SIGNATURE = "offer(Ljava/lang/Object;JLjava/util/concurrent/TimeUnit;)Z";
098: public final static String TAKE_SIGNATURE = "take()Ljava/lang/Object;";
099: public final static String POLL_TIMEOUT_SIGNATURE = "poll(JLjava/util/concurrent/TimeUnit;)Ljava/lang/Object;";
100: public final static String POLL_SIGNATURE = "poll()Ljava/lang/Object;";
101: public final static String DRAIN_TO_SIGNATURE = "drainTo(Ljava/util/Collection;)I";
102: public final static String DRAIN_TO_N_SIGNATURE = "drainTo(Ljava/util/Collection;I)I";
103: public final static String REMOVE_FIRST_N_SIGNATURE = "removeFirst(I)V";
104: public final static String PUT_IF_ABSENT_SIGNATURE = "putIfAbsent(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;";
105: public final static String REMOVE_IF_VALUE_EQUAL_SIGNATURE = "remove(Ljava/lang/Object;Ljava/lang/Object;)Z";
106: public final static String REPLACE_SIGNATURE = "replace(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;";
107: public final static String REPLACE_IF_VALUE_EQUAL_SIGNATURE = "replace(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Z";
108: public final static String SEGMENT_FOR_SIGNATURE = "segmentFor(I)Ljava/util/concurrent/ConcurrentHashMap$Segment;";
109: public final static String CONTAINS_VALUE_SIGNATURE = "containsValue(Ljava/lang/Object;)Z";
110: public final static String SIZE_SIGNATURE = "size()I";
111: public final static String IS_EMPTY_SIGNATURE = "isEmpty()Z";
112: public final static String SIGNAL_SIGNATURE = "signal()V";
113: public final static String SIGNAL_ALL_SIGNATURE = "signalAll()V";
114: public final static String TRANSFORM_VALUES_SIGNATURE = "transformValues(Lgnu/trove/TObjectFunction;)V";
115: public final static String URL_SET_SIGNATURE = "set(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V";
116:
117: private final TObjectIntHashMap mappings = new TObjectIntHashMap();
118:
119: public SerializationUtil() {
120: mappings.put(SET_ELEMENT_SIGNATURE, SET_ELEMENT);
121: mappings.put(SET_SIZE_SIGNATURE, SET_SIZE);
122: mappings.put(TRIM_TO_SIZE_SIGNATURE, TRIM_TO_SIZE);
123: mappings.put(REMOVE_ALL_ELEMENTS_SIGNATURE, CLEAR);
124: mappings.put(REMOVE_ELEMENT_AT_SIGNATURE, REMOVE_AT);
125: mappings.put(REMOVE_ELEMENT_SIGNATURE, REMOVE);
126: mappings.put(ADD_ELEMENT_SIGNATURE, ADD);
127: mappings.put(INSERT_ELEMENT_AT_SIGNATURE, INSERT_AT);
128: mappings.put(ADD_AT_SIGNATURE, ADD_AT);
129: mappings.put(ADD_ALL_AT_SIGNATURE, ADD_AT);
130: mappings.put(ADD_SIGNATURE, ADD);
131: mappings.put(ADD_ALL_SIGNATURE, ADD);
132: mappings.put(ADD_LAST_SIGNATURE, ADD_LAST);
133: mappings.put(ADD_FIRST_SIGNATURE, ADD_FIRST);
134: mappings.put(CLEAR_SIGNATURE, CLEAR);
135: mappings.put(PUT_SIGNATURE, PUT);
136: mappings.put(GET_SIGNATURE, GET);
137: mappings.put(SET_SIGNATURE, SET);
138: mappings.put(REMOVE_AT_SIGNATURE, REMOVE_AT);
139: mappings.put(REMOVE_SIGNATURE, REMOVE);
140: mappings.put(REMOVE_IF_VALUE_EQUAL_SIGNATURE,
141: REMOVE_IF_VALUE_EQUAL);
142: mappings.put(REMOVE_KEY_SIGNATURE, REMOVE);
143: mappings.put(TROVE_REMOVE_AT_SIGNATURE, REMOVE);
144: mappings.put(REMOVE_ENTRY_FOR_KEY_SIGNATURE, REMOVE);
145: mappings.put(REMOVE_FIRST_SIGNATURE, REMOVE_FIRST);
146: mappings.put(REMOVE_LAST_SIGNATURE, REMOVE_LAST);
147: mappings.put(REMOVE_RANGE_SIGNATURE, REMOVE_RANGE);
148: mappings.put(SET_TIME_SIGNATURE, SET_TIME);
149: mappings.put(SET_NANOS_SIGNATURE, SET_NANOS);
150: mappings.put(REMOVE_ALL_SIGNATURE, REMOVE_ALL);
151: mappings.put(QUEUE_PUT_SIGNATURE, PUT);
152: mappings.put(TAKE_SIGNATURE, TAKE);
153: mappings.put(REMOVE_FIRST_N_SIGNATURE, REMOVE_FIRST_N);
154: mappings.put(PUT_IF_ABSENT_SIGNATURE, PUT_IF_ABSENT);
155: mappings.put(REPLACE_SIGNATURE, REPLACE);
156: mappings.put(REPLACE_IF_VALUE_EQUAL_SIGNATURE,
157: REPLACE_IF_VALUE_EQUAL);
158: mappings.put(URL_SET_SIGNATURE, URL_SET);
159: }
160:
161: public String[] getSignatures() {
162: String[] rv = new String[this .mappings.size()];
163: int index = 0;
164: for (TObjectIntIterator i = mappings.iterator(); i.hasNext(); index++) {
165: i.advance();
166: rv[index] = (String) i.key();
167: }
168: return rv;
169: }
170:
171: public int methodToID(String name) {
172: int i = mappings.get(name);
173: if (i == 0)
174: throw new AssertionError("Illegal method name:" + name);
175: return i;
176: }
177:
178: private static final Pattern PARENT_FIELD_PATTERN = Pattern
179: .compile("^this\\$\\d+$");
180:
181: public boolean isParent(String fieldName) {
182: return PARENT_FIELD_PATTERN.matcher(fieldName).matches();
183: }
184: }
|