Enumerate.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package commons.enumerate;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. public class Enumerate implements Comparable,Serializable {
  7. private static final long serialVersionUID = 1L;
  8. private int m_ordinal;
  9. private String m_name;
  10. public Enumerate(){
  11. }
  12. protected Enumerate(String name, int ordinal) {
  13. m_ordinal = ordinal;
  14. m_name = name;
  15. }
  16. private void checkClass(Class other)
  17. {
  18. if(!other.equals(getClass()))
  19. {
  20. throw new ClassCastException("Couldn't compare a " + other.getClass() + " to " + this.getClass());
  21. }
  22. }
  23. public int compareTo(Object o) {
  24. checkClass(o.getClass());
  25. return ((Enumerate) o).getOrdinal() - getOrdinal();
  26. }
  27. @Override
  28. public boolean equals(Object o) {
  29. if(null == o){
  30. return false;
  31. }
  32. if(o instanceof String)
  33. return getName().equals(o);
  34. if(o.getClass().equals(getClass()))
  35. return equals(((Enumerate)o).getOrdinal());
  36. return super.equals(o);
  37. }
  38. public boolean equals(int value) {
  39. return getOrdinal() == value;
  40. }
  41. public String getName() {
  42. return m_name;
  43. }
  44. public byte getOrdinal() {
  45. return (byte)m_ordinal;
  46. }
  47. private static Enumerate getNullValue(Class theClass) throws NullPointerException
  48. {
  49. // Cerca il campo marcato con il nome di NULLO
  50. Field fields[] = theClass.getFields();
  51. for (int i = 0; i < fields.length; i++)
  52. {
  53. Field field = fields[i];
  54. if(field.getName().equalsIgnoreCase("NULLO"))
  55. {
  56. try
  57. {
  58. return (Enumerate)field.get(null);
  59. }
  60. catch (IllegalArgumentException e) {}
  61. catch (IllegalAccessException e) {}
  62. }
  63. }
  64. throw new NullPointerException("Object NULLO not found in class " + theClass.getName());
  65. }
  66. /**
  67. * @param nclass La classe enumerata su cui fare la ricerca
  68. * @param b Il valore dell'ordinale da ricercare
  69. * @return Il valore dell'enumerato,null se non esiste
  70. */
  71. public static Enumerate getValue(Class nclass, int b)
  72. {
  73. try {
  74. Method metodo = nclass.getMethod("values", (Class[])null);
  75. Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null);
  76. for (int i = 0; i < enums.length; i++) {
  77. if (enums[i].getOrdinal() == b) {
  78. return enums[i];
  79. }
  80. }
  81. return getNullValue(nclass);
  82. } catch (SecurityException e) {
  83. throw new InvalidEnumerateException(nclass);
  84. } catch (IllegalArgumentException e) {
  85. throw new InvalidEnumerateException(nclass);
  86. } catch (NoSuchMethodException e) {
  87. throw new InvalidEnumerateException(nclass);
  88. } catch (IllegalAccessException e) {
  89. throw new InvalidEnumerateException(nclass);
  90. } catch (InvocationTargetException e) {
  91. throw new InvalidEnumerateException(nclass);
  92. }
  93. }
  94. /**
  95. * @param nclass La classe enumerata su cui fare la ricerca
  96. * @param b Il valore testuale su cui fare la ricerca;se ? un valore numerico in formato
  97. * stringa allora viene usata la versione di ricerca sull'ordinale,altrimenti quella
  98. * di ricerca sul nome
  99. * @return Il valore dell'enumerato,null se non esiste
  100. */
  101. public static Enumerate getValue(Class theClass,String value)
  102. {
  103. try
  104. {
  105. // Se la stringa ? un numero allora utilizza la versione che ricerca l'ordinale
  106. return getValue(theClass,Integer.parseInt(value));
  107. }
  108. catch(NumberFormatException nfe)
  109. {
  110. }
  111. try
  112. {
  113. Method metodo = theClass.getMethod("values", (Class[])null);
  114. Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null);
  115. for (int i = 0; i < enums.length; i++) {
  116. if (enums[i].getName().equals(value)) {
  117. return enums[i];
  118. }
  119. }
  120. return getNullValue(theClass);
  121. } catch (SecurityException e) {
  122. throw new InvalidEnumerateException(theClass);
  123. } catch (IllegalArgumentException e) {
  124. throw new InvalidEnumerateException(theClass);
  125. } catch (NoSuchMethodException e) {
  126. throw new InvalidEnumerateException(theClass);
  127. } catch (IllegalAccessException e) {
  128. throw new InvalidEnumerateException(theClass);
  129. } catch (InvocationTargetException e) {
  130. throw new InvalidEnumerateException(theClass);
  131. }
  132. }
  133. @Override
  134. public String toString(){
  135. return String.valueOf(getOrdinal());
  136. }
  137. @Override
  138. public int hashCode()
  139. {
  140. final int PRIME = 31;
  141. int result = 1;
  142. result = PRIME * result + new Integer(m_ordinal).hashCode();
  143. return result;
  144. }
  145. }