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.objectserver.mgmt;
005:
006: import com.tc.object.ObjectID;
007:
008: import java.io.Serializable;
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Collections;
012: import java.util.List;
013: import java.util.Map;
014:
015: public class PhysicalManagedObjectFacade extends AbstractObjectFacade
016: implements Serializable {
017: private static final String[] EMPTY_STRING_ARRAY = new String[] {};
018:
019: private final String className;
020: private final Map fields;
021: private final ObjectID objectID;
022: private final ObjectID parentID;
023: private final boolean isInner;
024: private final int arrayLength;
025: private final boolean isArray;
026: private final String[] fieldNames;
027:
028: public PhysicalManagedObjectFacade(ObjectID id, ObjectID parentID,
029: String className, Map data, boolean isInner,
030: int arrayLength, boolean isArray) {
031: this .className = className;
032: this .fields = Collections.unmodifiableMap(data);
033: this .objectID = id;
034: this .parentID = parentID;
035: this .isInner = isInner;
036: this .isArray = isArray;
037: this .arrayLength = arrayLength;
038: this .fieldNames = sortFieldNames((String[]) this .fields
039: .keySet().toArray(EMPTY_STRING_ARRAY));
040: }
041:
042: private String[] sortFieldNames(String[] names) {
043: List special = new ArrayList();
044: List regular = new ArrayList();
045:
046: for (int i = 0; i < names.length; i++) {
047: if (names[i].indexOf('$') >= 0) {
048: special.add(names[i]);
049: } else {
050: regular.add(names[i]);
051: }
052: }
053:
054: String[] first = (String[]) special.toArray(EMPTY_STRING_ARRAY);
055: String[] second = (String[]) regular
056: .toArray(EMPTY_STRING_ARRAY);
057: Arrays.sort(first);
058: Arrays.sort(second);
059:
060: String[] rv = new String[names.length];
061: System.arraycopy(first, 0, rv, 0, first.length);
062: System.arraycopy(second, 0, rv, first.length, second.length);
063:
064: return rv;
065: }
066:
067: public String getClassName() {
068: return this .className;
069: }
070:
071: public String[] getFields() {
072: return (String[]) this .fieldNames.clone();
073: }
074:
075: protected Object basicGetFieldValue(String fieldName) {
076: checkValidName(fieldName);
077: return this .fields.get(fieldName);
078: }
079:
080: public boolean isPrimitive(String fieldName) {
081: Object value = getFieldValue(fieldName);
082: return !(value instanceof ObjectID);
083: }
084:
085: public ObjectID getObjectId() {
086: return this .objectID;
087: }
088:
089: public boolean isInnerClass() {
090: return this .isInner;
091: }
092:
093: public ObjectID getParentObjectId() {
094: if (!this .isInner) {
095: throw new IllegalStateException("Not an inner class");
096: }
097: return this .parentID;
098: }
099:
100: public boolean isArray() {
101: return this .isArray;
102: }
103:
104: public int getArrayLength() {
105: if (!this .isArray) {
106: throw new IllegalStateException("Not an array");
107: }
108: return this .arrayLength;
109: }
110:
111: public boolean isList() {
112: return false;
113: }
114:
115: public boolean isSet() {
116: return false;
117: }
118:
119: public boolean isMap() {
120: return false;
121: }
122:
123: public int getFacadeSize() {
124: throw new UnsupportedOperationException("Not a collection");
125: }
126:
127: public int getTrueObjectSize() {
128: throw new UnsupportedOperationException("Not a collection");
129: }
130:
131: private void checkValidName(String fieldName) {
132: if (this .fields.containsKey(fieldName)) {
133: return;
134: }
135: throw new IllegalArgumentException(className + "." + fieldName
136: + " does not exist");
137: }
138:
139: }
|