~ubuntu-branches/ubuntu/oneiric/postgresql-pljava/oneiric

« back to all changes in this revision

Viewing changes to src/java/pljava/org/postgresql/pljava/internal/Oid.java

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut
  • Date: 2006-06-26 10:44:55 UTC
  • mfrom: (1.1.1 upstream) (3.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20060626104455-135i9wosat2k8vvt
Tags: 1.3.0-1
* New upstream release (closes: #375199)
* Built for postgresql 8.1 (closes: #339641)
* Rebuilt for new libgcj library (closes: #369986)
* Updated copyright file
* Updated standards version
* Made use of cdbs simple patchsys

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2004, 2005 TADA AB - Taby Sweden
 
2
 * Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
3
3
 * Distributed under the terms shown in the file COPYRIGHT
4
4
 * found in the root folder of this project or at
5
5
 * http://eng.tada.se/osprojects/COPYRIGHT.html
16
16
 *
17
17
 * @author Thomas Hallgren
18
18
 */
19
 
public class Oid
 
19
public class Oid extends Number
20
20
{
 
21
        private static final HashMap s_class2typeId = new HashMap();
 
22
 
 
23
        private static final HashMap s_typeId2class = new HashMap();
21
24
        static
22
25
        {
23
26
                try
33
36
                }
34
37
        }
35
38
 
36
 
        private static final HashMap s_class2typeId = new HashMap();
37
 
        private static final HashMap s_typeId2class = new HashMap();
 
39
        /**
 
40
         * Finds the PostgreSQL well known Oid for the given class.
 
41
         * @param clazz The class.
 
42
         * @return The well known Oid or null if no such Oid could be found.
 
43
         */
 
44
        public static Oid forJavaClass(Class clazz)
 
45
        {
 
46
                return (Oid)s_class2typeId.get(clazz);
 
47
        }
 
48
 
 
49
        /**
 
50
         * Finds the PostgreSQL well known Oid for a type name.
 
51
         * @param typeString The name of the type, optionally qualified with a namespace.
 
52
         * @return The well known Oid.
 
53
         * @throws SQLException if the type could not be found
 
54
         */
 
55
        public static Oid forTypeName(String typeString)
 
56
        {
 
57
                synchronized(Backend.THREADLOCK)
 
58
                {
 
59
                        return new Oid(_forTypeName(typeString));
 
60
                }
 
61
        }
 
62
 
 
63
        /**
 
64
         * Finds the PostgreSQL well known Oid for the XOPEN Sql type.
 
65
         * @param sqlType The XOPEN type code.
 
66
         * @throws SQLException if the type could not be found
 
67
         */
 
68
        public static Oid forSqlType(int sqlType)
 
69
        {
 
70
                synchronized(Backend.THREADLOCK)
 
71
                {
 
72
                        return new Oid(_forSqlType(sqlType));
 
73
                }
 
74
        }
 
75
 
 
76
        /**
 
77
         * Returns the PostgreSQL type id for the Oid type.
 
78
         */
 
79
        public static Oid getTypeId()
 
80
        {
 
81
                synchronized(Backend.THREADLOCK)
 
82
                {
 
83
                        return _getTypeId();
 
84
                }
 
85
        }
 
86
 
 
87
        /**
 
88
         * A Type well known to PostgreSQL but not known as a standard XOPEN
 
89
         * SQL type can be registered here. This includes types like the Oid
 
90
         * itself and all the geometry related types.
 
91
         * @param clazz The Java class that corresponds to the type id.
 
92
         * @param typeId The well known type id.
 
93
         */
 
94
        public static void registerType(Class clazz, Oid typeId)
 
95
        {
 
96
                s_class2typeId.put(clazz, typeId);
 
97
                if(!s_typeId2class.containsKey(typeId))
 
98
                        s_typeId2class.put(typeId, clazz);
 
99
        }
38
100
 
39
101
        /*
40
102
         * The native Oid represented as a 32 bit quantity.
48
110
                m_native = value;
49
111
        }
50
112
 
 
113
        public double doubleValue()
 
114
        {
 
115
                return m_native;
 
116
        }
 
117
 
51
118
        /**
52
119
         * Checks to see if the other object is an <code>Oid</code>, and if so,
53
120
         * if the native value of that <code>Oid</code> equals the native value
59
126
                return (o == this) || ((o instanceof Oid) && ((Oid)o).m_native == m_native);
60
127
        }
61
128
 
 
129
        public float floatValue()
 
130
        {
 
131
                return m_native;
 
132
        }
 
133
 
 
134
        public Class getJavaClass()
 
135
        throws SQLException
 
136
        {
 
137
                Class c = (Class)s_typeId2class.get(this);
 
138
                if(c == null)
 
139
                {
 
140
                        String className;
 
141
                        synchronized(Backend.THREADLOCK)
 
142
                        {
 
143
                                className = _getJavaClassName(m_native);
 
144
                        }
 
145
                        try
 
146
                        {
 
147
                                c = Class.forName(getCanonicalClassName(className, 0));
 
148
                        }
 
149
                        catch(ClassNotFoundException e)
 
150
                        {
 
151
                                throw new SQLException(e.getMessage());
 
152
                        }
 
153
                        s_typeId2class.put(this, c);
 
154
                        s_class2typeId.put(c, this);
 
155
                }
 
156
                return c;
 
157
        }
 
158
 
 
159
        /**
 
160
         * The native value is used as the hash code.
 
161
         * @return The hashCode for this <code>Oid</code>.
 
162
         */
 
163
        public int hashCode()
 
164
        {
 
165
                return m_native;
 
166
        }
 
167
 
 
168
        public int intValue()
 
169
        {
 
170
                return m_native;
 
171
        }
 
172
 
 
173
        public long longValue()
 
174
        {
 
175
                return m_native;
 
176
        }
 
177
 
 
178
        /**
 
179
         * Returns a string representation of this OID.
 
180
         */
 
181
        public String toString()
 
182
        {
 
183
                return "OID(" + m_native + ')';
 
184
        }
 
185
 
62
186
        private static String getCanonicalClassName(String name, int nDims)
63
187
        {
64
188
                if(name.endsWith("[]"))
102
226
                return name;
103
227
        }
104
228
 
105
 
        public Class getJavaClass()
106
 
        throws SQLException
107
 
        {
108
 
                Class c = (Class)s_typeId2class.get(this);
109
 
                if(c == null)
110
 
                {
111
 
                        String className;
112
 
                        synchronized(Backend.THREADLOCK)
113
 
                        {
114
 
                                className = this._getJavaClassName();
115
 
                        }
116
 
                        try
117
 
                        {
118
 
                                c = Class.forName(getCanonicalClassName(className, 0));
119
 
                        }
120
 
                        catch(ClassNotFoundException e)
121
 
                        {
122
 
                                throw new SQLException(e.getMessage());
123
 
                        }
124
 
                        s_typeId2class.put(this, c);
125
 
                        s_class2typeId.put(c, this);
126
 
                }
127
 
                return c;
128
 
        }
129
 
 
130
 
        /**
131
 
         * The native value is used as the hash code.
132
 
         * @return The hashCode for this <code>Oid</code>.
133
 
         */
134
 
        public int hashCode()
135
 
        {
136
 
                return m_native;
137
 
        }
138
 
 
139
 
        /**
140
 
         * A Type well known to PostgreSQL but not known as a standard XOPEN
141
 
         * SQL type can be registered here. This includes types like the Oid
142
 
         * itself and all the geometry related types.
143
 
         * @param clazz The Java class that corresponds to the type id.
144
 
         * @param typeId The well known type id.
145
 
         */
146
 
        public static void registerType(Class clazz, Oid typeId)
147
 
        {
148
 
                s_class2typeId.put(clazz, typeId);
149
 
                if(!s_typeId2class.containsKey(typeId))
150
 
                        s_typeId2class.put(typeId, clazz);
151
 
        }
152
 
 
153
 
        /**
154
 
         * Finds the PostgreSQL well known Oid for the given class.
155
 
         * @param clazz The class.
156
 
         * @return The well known Oid or null if no such Oid could be found.
157
 
         */
158
 
        public static Oid forJavaClass(Class clazz)
159
 
        {
160
 
                return (Oid)s_class2typeId.get(clazz);
161
 
        }
162
 
 
163
 
        /**
164
 
         * Finds the PostgreSQL well known Oid for the XOPEN Sql type.
165
 
         * @param sqlType The XOPEN type code.
166
 
         * @return The well known Oid or null if no such Oid could be found.
167
 
         */
168
 
        public static Oid forSqlType(int sqlType)
169
 
        {
170
 
                synchronized(Backend.THREADLOCK)
171
 
                {
172
 
                        return _forSqlType(sqlType);
173
 
                }
174
 
        }
175
 
 
176
 
        /**
177
 
         * Returns the PostgreSQL type id for the Oid type.
178
 
         */
179
 
        public static Oid getTypeId()
180
 
        {
181
 
                synchronized(Backend.THREADLOCK)
182
 
                {
183
 
                        return _getTypeId();
184
 
                }
185
 
        }
186
 
 
187
 
        /**
188
 
         * Returns a string representation of this OID.
189
 
         */
190
 
        public String toString()
191
 
        {
192
 
                return "OID(" + m_native + ')';
193
 
        }
194
 
 
195
 
        private native static Oid _forSqlType(int sqlType);
 
229
        private native static int _forTypeName(String typeString);
 
230
 
 
231
        private native static int _forSqlType(int sqlType);
 
232
 
196
233
        private native static Oid _getTypeId();
197
234
 
198
 
        private native String _getJavaClassName()
 
235
        private native static String _getJavaClassName(int nativeOid)
199
236
        throws SQLException;
200
237
}