KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > mgmt > PhysicalManagedObjectFacade


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.mgmt;
5
6 import com.tc.object.ObjectID;
7
8 import java.io.Serializable JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 public class PhysicalManagedObjectFacade extends AbstractObjectFacade implements Serializable JavaDoc {
16   private static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[] {};
17
18   private final String JavaDoc className;
19   private final Map JavaDoc fields;
20   private final ObjectID objectID;
21   private final ObjectID parentID;
22   private final boolean isInner;
23   private final int arrayLength;
24   private final boolean isArray;
25   private final String JavaDoc[] fieldNames;
26
27   public PhysicalManagedObjectFacade(ObjectID id, ObjectID parentID, String JavaDoc className, Map JavaDoc data, boolean isInner,
28                                      int arrayLength, boolean isArray) {
29     this.className = className;
30     this.fields = Collections.unmodifiableMap(data);
31     this.objectID = id;
32     this.parentID = parentID;
33     this.isInner = isInner;
34     this.isArray = isArray;
35     this.arrayLength = arrayLength;
36     this.fieldNames = sortFieldNames((String JavaDoc[]) this.fields.keySet().toArray(EMPTY_STRING_ARRAY));
37   }
38
39   private String JavaDoc[] sortFieldNames(String JavaDoc[] names) {
40     List JavaDoc special = new ArrayList JavaDoc();
41     List JavaDoc regular = new ArrayList JavaDoc();
42
43     for (int i = 0; i < names.length; i++) {
44       if (names[i].indexOf('$') >= 0) {
45         special.add(names[i]);
46       } else {
47         regular.add(names[i]);
48       }
49     }
50
51     String JavaDoc[] first = (String JavaDoc[]) special.toArray(EMPTY_STRING_ARRAY);
52     String JavaDoc[] second = (String JavaDoc[]) regular.toArray(EMPTY_STRING_ARRAY);
53     Arrays.sort(first);
54     Arrays.sort(second);
55
56     String JavaDoc[] rv = new String JavaDoc[names.length];
57     System.arraycopy(first, 0, rv, 0, first.length);
58     System.arraycopy(second, 0, rv, first.length, second.length);
59
60     return rv;
61   }
62
63   public String JavaDoc getClassName() {
64     return this.className;
65   }
66
67   public String JavaDoc[] getFields() {
68     return (String JavaDoc[]) this.fieldNames.clone();
69   }
70
71   protected Object JavaDoc basicGetFieldValue(String JavaDoc fieldName) {
72     checkValidName(fieldName);
73     return this.fields.get(fieldName);
74   }
75
76   public boolean isPrimitive(String JavaDoc fieldName) {
77     Object JavaDoc value = getFieldValue(fieldName);
78     return !(value instanceof ObjectID);
79   }
80
81   public ObjectID getObjectId() {
82     return this.objectID;
83   }
84
85   public boolean isInnerClass() {
86     return this.isInner;
87   }
88
89   public ObjectID getParentObjectId() {
90     if (!this.isInner) { throw new IllegalStateException JavaDoc("Not an inner class"); }
91     return this.parentID;
92   }
93
94   public boolean isArray() {
95     return this.isArray;
96   }
97
98   public int getArrayLength() {
99     if (!this.isArray) { throw new IllegalStateException JavaDoc("Not an array"); }
100     return this.arrayLength;
101   }
102
103   public boolean isList() {
104     return false;
105   }
106
107   public boolean isSet() {
108     return false;
109   }
110
111   public boolean isMap() {
112     return false;
113   }
114
115   public int getFacadeSize() {
116     throw new UnsupportedOperationException JavaDoc("Not a collection");
117   }
118
119   public int getTrueObjectSize() {
120     throw new UnsupportedOperationException JavaDoc("Not a collection");
121   }
122
123   private void checkValidName(String JavaDoc fieldName) {
124     if (this.fields.containsKey(fieldName)) { return; }
125     throw new IllegalArgumentException JavaDoc(className + "." + fieldName + " does not exist");
126   }
127
128 }
129
Popular Tags