View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.pool.sizeof;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /***
23   * Enum with all the flyweight types that we check for sizeOf measurements
24   *
25   * @author Alex Snaps
26   */
27  enum FlyweightType {
28  
29      /***
30       * java.lang.Enum
31       */
32      ENUM(Enum.class) {
33          @Override
34          boolean isShared(final Object obj) { return true; }
35      },
36      // XXX There is no nullipotent way of determining the interned status of a string 
37      /***
38       * java.lang.String
39       */
40      //STRING(String.class) {
41      //    @Override
42      //    boolean isShared(final Object obj) { return obj == ((String)obj).intern(); }
43      //},
44      /***
45       * java.lang.Boolean
46       */
47      BOOLEAN(Boolean.class) {
48          @Override
49          boolean isShared(final Object obj) { return obj == Boolean.TRUE || obj == Boolean.FALSE; }
50      },
51      /***
52       * java.lang.Integer
53       */
54      INTEGER(Integer.class) {
55          @Override
56          boolean isShared(final Object obj) { return obj == Integer.valueOf((Integer)obj); }
57      },
58      /***
59       * java.lang.Short
60       */
61      SHORT(Short.class) {
62          @Override
63          boolean isShared(final Object obj) { return obj == Short.valueOf((Short)obj); }
64      },
65      /***
66       * java.lang.Byte
67       */
68      BYTE(Byte.class) {
69          @Override
70          boolean isShared(final Object obj) { return obj == Byte.valueOf((Byte)obj); }
71      },
72      /***
73       * java.lang.Long
74       */
75      LONG(Long.class) {
76          @Override
77          boolean isShared(final Object obj) { return obj == Long.valueOf((Long)obj); }
78      },
79      /***
80       * java.lang.Character
81       */
82      CHARACTER(Character.class) {
83          @Override
84          boolean isShared(final Object obj) { return obj == Character.valueOf((Character)obj); }
85      };
86  
87      private static final Map<Class<?>, FlyweightType> TYPE_MAPPINGS = new HashMap<Class<?>, FlyweightType>();
88      static {
89          for (FlyweightType type : FlyweightType.values()) {
90            TYPE_MAPPINGS.put(type.clazz, type);
91          }
92      }
93      
94      private final Class<?> clazz;
95  
96      private FlyweightType(final Class<?> clazz) {
97          this.clazz = clazz;
98      }
99  
100     /***
101      * Whether this is a shared object
102      * @param obj the object to check for
103      * @return true, if shared
104      */
105     abstract boolean isShared(Object obj);
106 
107     /***
108      * Will return the Flyweight enum instance for the flyweight Class, or null if type isn't flyweight
109      * @param aClazz the class we need the FlyweightType instance for
110      * @return the FlyweightType, or null
111      */
112     static FlyweightType getFlyweightType(final Class<?> aClazz) {
113         if (aClazz.isEnum()) {
114             return ENUM;
115         } else {
116             return TYPE_MAPPINGS.get(aClazz);
117         }
118     }
119 }