001    package com.nativelibs4java.velocity;
002    
003    import java.nio.*;
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    
008    /**
009     *
010     * @author Olivier
011     */
012    public abstract class Primitive {
013    
014        private final Class<?> type, wrapper, bufferType;
015        private final String size;
016        private final String name, capName, wrapperName, bufferName;
017        //private final String v1, v2, v3;
018    
019        public String getCapName() {
020            return capName;
021        }
022    
023        public Class<?> getBufferType() {
024            return bufferType;
025        }
026    
027        public boolean isSignedIntegral() {
028            return type == int.class
029                    || type == short.class
030                    || type == long.class
031                    || type == byte.class;
032        }
033    
034        public String getName() {
035            return name;
036        }
037    
038        public String getTypeRef() {
039            return getWrapperName();
040        }
041    
042        public String getRawTypeRef() {
043            return getTypeRef();
044        }
045        /*
046         public String getV1() {
047         return       v1;
048         }
049    
050         public String getV2() {
051         return       v2;
052         }
053    
054         public String getV3() {
055         return       v3;
056         }
057         */
058    
059        public String getSize() {
060            return size;
061        }
062    
063        public Class<?> getType() {
064            return type;
065        }
066    
067        public Class<?> getWrapper() {
068            return wrapper;
069        }
070    
071        public String getWrapperName() {
072            return wrapperName;
073        }
074    
075        public String getBufferName() {
076            return bufferName;
077        }
078    
079        public String value(String intValue) {
080            return intValue;
081        }
082    
083        public String rawValue(String intValue) {
084            return value(intValue);
085        }
086    
087        public Primitive(String integralClassName) {
088            this.type = null;
089            wrapper = null;
090            bufferType = null;
091            size = integralClassName + ".SIZE";
092            //v1 = "new " + integralClassName + "(1)";
093            //v2 = "new " + integralClassName + "(2)";
094            //v3 = "new " + integralClassName + "(3)";
095    
096            name = integralClassName;
097            capName = integralClassName;
098            wrapperName = integralClassName;
099            bufferName = null;
100        }
101    
102        public Primitive(Class<?> type) {
103            this.type = type;
104            if (type == Integer.TYPE) {
105                wrapper = Integer.class;
106                bufferType = IntBuffer.class;
107                size = "4";
108                //v1 = "1";
109                //v2 = "2";
110                //v3 = "3";
111            } else if (type == Long.TYPE) {
112                wrapper = Long.class;
113                bufferType = LongBuffer.class;
114                size = "8";
115                //v1 = "1L";
116                //v2 = "2L";
117                //v3 = "3L";
118            } else if (type == Short.TYPE) {
119                wrapper = Short.class;
120                bufferType = ShortBuffer.class;
121                size = "2";
122                //v1 = "(short)1";
123                //v2 = "(short)2";
124                //v3 = "(short)3";
125            } else if (type == Byte.TYPE) {
126                wrapper = Byte.class;
127                bufferType = ByteBuffer.class;
128                size = "1";
129                //v1 = "(byte)1";
130                //v2 = "(byte)2";
131                //v3 = "(byte)3";
132            } else if (type == Character.TYPE) {
133                wrapper = Character.class;
134                bufferType = CharBuffer.class;
135                size = "2";
136                //v1 = "'a'";
137                //v2 = "'b'";
138                //v3 = "'c'";
139            } else if (type == Float.TYPE) {
140                wrapper = Float.class;
141                bufferType = FloatBuffer.class;
142                size = "4";
143                //v1 = "1f";
144                //v2 = "2f";
145                //v3 = "3f";
146            } else if (type == Double.TYPE) {
147                wrapper = Double.class;
148                bufferType = DoubleBuffer.class;
149                size = "8";
150                //v1 = "1.0";
151                //v2 = "2.0";
152                //v3 = "3.0";
153            } else if (type == Boolean.TYPE) {
154                wrapper = Boolean.class;
155                bufferType = ByteBuffer.class;
156                size = "1";
157                //v1 = "true";
158                //v2 = "false";
159                //v3 = "true";
160            } else {
161                throw new IllegalArgumentException();
162            }
163    
164            name = type.getName();
165            capName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
166            wrapperName = wrapper.getSimpleName();
167            bufferName = bufferType.getSimpleName();
168        }
169        static List<Primitive> bridJPrimitives;
170    
171        public static synchronized List<Primitive> getBridJPrimitives() {
172            if (bridJPrimitives == null) {
173                bridJPrimitives = new ArrayList<Primitive>();
174                bridJPrimitives.addAll(getPrimitives());
175                bridJPrimitives.add(new Primitive("CLong") {
176                    public String value(String intValue) {
177                        return "new CLong(" + intValue + ")";
178                    }
179    
180                    public String rawValue(String intValue) {
181                        return "(long)" + intValue;
182                    }
183                });
184                bridJPrimitives.add(new Primitive("SizeT") {
185                    public String value(String intValue) {
186                        return "new SizeT(" + intValue + ")";
187                    }
188    
189                    public String rawValue(String intValue) {
190                        return "(long)" + intValue;
191                    }
192                });
193                bridJPrimitives.add(new Primitive("Pointer") {
194                    public String value(String intValue) {
195                        return "(Pointer)Pointer.pointerToAddress(" + intValue + ")";
196                    }
197    
198                    public String getTypeRef() {
199                        return "Pointer<?>";
200                    }
201    
202                    public String getRawTypeRef() {
203                        return "Pointer";
204                    }
205                });
206                bridJPrimitives = Collections.unmodifiableList(bridJPrimitives);
207            }
208            return bridJPrimitives;
209        }
210        static List<Primitive> primitives;
211    
212        public static synchronized List<Primitive> getPrimitives() {
213            if (primitives == null) {
214                primitives = new ArrayList<Primitive>();
215                primitives.addAll(getPrimitivesNoBool());
216                primitives.add(new Primitive(Boolean.TYPE) {
217                    public String value(String intValue) {
218                        return "(((" + intValue + ") % 2) == 1)";
219                    }
220                });
221                primitives = Collections.unmodifiableList(primitives);
222            }
223            return primitives;
224        }
225        static List<Primitive> primitivesNoBool;
226    
227        public static synchronized List<Primitive> getPrimitivesNoBool() {
228            if (primitivesNoBool == null) {
229                primitivesNoBool = new ArrayList<Primitive>();
230                primitivesNoBool.add(new Primitive(Integer.TYPE) {
231                    public String value(String intValue) {
232                        return intValue;
233                    }
234                });
235                primitivesNoBool.add(new Primitive(Long.TYPE) {
236                    public String value(String intValue) {
237                        return "(long)" + intValue;
238                    }
239                });
240                primitivesNoBool.add(new Primitive(Short.TYPE) {
241                    public String value(String intValue) {
242                        return "(short)" + intValue;
243                    }
244                });
245                primitivesNoBool.add(new Primitive(Byte.TYPE) {
246                    public String value(String intValue) {
247                        return "(byte)" + intValue;
248                    }
249                });
250                primitivesNoBool.add(new Primitive(Character.TYPE) {
251                    public String value(String intValue) {
252                        return "(char)" + intValue;
253                    }
254                });
255                primitivesNoBool.add(new Primitive(Float.TYPE) {
256                    public String value(String intValue) {
257                        return "(float)" + intValue;
258                    }
259                });
260                primitivesNoBool.add(new Primitive(Double.TYPE) {
261                    public String value(String intValue) {
262                        return "(double)" + intValue;
263                    }
264                });
265                primitivesNoBool = Collections.unmodifiableList(primitivesNoBool);
266            }
267            return primitivesNoBool;
268        }
269    }