KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > FieldInfo


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: FieldInfo.java,v 1.18 2006/12/05 01:35:37 mark Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import java.io.Serializable JavaDoc;
12 import java.lang.reflect.Field JavaDoc;
13 import java.lang.reflect.Modifier JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import com.sleepycat.persist.raw.RawField;
19
20 /**
21  * A field definition used by ComplexFormat and CompositeKeyFormat.
22  *
23  * @author Mark Hayes
24  */

25 class FieldInfo implements RawField, Serializable JavaDoc, Comparable JavaDoc<FieldInfo> {
26     
27     private static final long serialVersionUID = 2062721100372306296L;
28
29     /**
30      * Returns a list of all non-transient non-static fields that are declared
31      * in the given class.
32      */

33     static List JavaDoc<FieldInfo> getInstanceFields(Class JavaDoc cls) {
34         Field JavaDoc[] declaredFields = cls.getDeclaredFields();
35         List JavaDoc<FieldInfo> fields =
36             new ArrayList JavaDoc<FieldInfo>(declaredFields.length);
37         for (Field JavaDoc field : declaredFields) {
38             int mods = field.getModifiers();
39             if (!Modifier.isTransient(mods) && !Modifier.isStatic(mods)) {
40                 fields.add(new FieldInfo(field));
41             }
42         }
43         return fields;
44     }
45
46     static FieldInfo getField(List JavaDoc<FieldInfo> fields, String JavaDoc fieldName) {
47         int i = getFieldIndex(fields, fieldName);
48         if (i >= 0) {
49             return fields.get(i);
50         } else {
51             return null;
52         }
53     }
54
55     static int getFieldIndex(List JavaDoc<FieldInfo> fields, String JavaDoc fieldName) {
56         for (int i = 0; i < fields.size(); i += 1) {
57             FieldInfo field = fields.get(i);
58             if (fieldName.equals(field.getName())) {
59                 return i;
60             }
61         }
62         return -1;
63     }
64
65     private String JavaDoc name;
66     private String JavaDoc className;
67     private Format format;
68     private transient Class JavaDoc cls;
69
70     private FieldInfo(Field JavaDoc field) {
71         name = field.getName();
72         cls = field.getType();
73         className = cls.getName();
74     }
75
76     void collectRelatedFormats(Catalog catalog,
77                                Map JavaDoc<String JavaDoc,Format> newFormats) {
78         format = catalog.createFormat(cls, newFormats);
79     }
80
81     void migrateFromBeta(Map JavaDoc<String JavaDoc,Format> formatMap) {
82         if (format == null) {
83             format = formatMap.get(className);
84             if (format == null) {
85                 throw new IllegalStateException JavaDoc(className);
86             }
87         }
88     }
89
90     void initialize(Catalog catalog) {
91     }
92
93     Class JavaDoc getFieldClass() {
94         if (cls == null) {
95             try {
96                 cls = SimpleCatalog.classForName(className);
97             } catch (ClassNotFoundException JavaDoc e) {
98                 throw new IllegalStateException JavaDoc(e);
99             }
100         }
101         return cls;
102     }
103
104     String JavaDoc getClassName() {
105         return className;
106     }
107
108     public String JavaDoc getName() {
109         return name;
110     }
111
112     public Format getType() {
113         return format;
114     }
115
116     public int compareTo(FieldInfo o) {
117         return name.compareTo(o.name);
118     }
119
120     @Override JavaDoc
121     public boolean equals(Object JavaDoc other) {
122         if (other instanceof FieldInfo) {
123             FieldInfo o = (FieldInfo) other;
124             return name.equals(o.name);
125         } else {
126             return false;
127         }
128     }
129
130     @Override JavaDoc
131     public int hashCode() {
132         return name.hashCode();
133     }
134
135     @Override JavaDoc
136     public String JavaDoc toString() {
137         return "[Field name: " + name + " class: " + className + ']';
138     }
139 }
140
Popular Tags