| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package commons.enumerate;
- import java.io.Serializable;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- public class Enumerate implements Comparable,Serializable {
- private static final long serialVersionUID = 1L;
- private int m_ordinal;
- private String m_name;
- public Enumerate(){
- }
- protected Enumerate(String name, int ordinal) {
- m_ordinal = ordinal;
- m_name = name;
- }
- private void checkClass(Class other)
- {
- if(!other.equals(getClass()))
- {
- throw new ClassCastException("Couldn't compare a " + other.getClass() + " to " + this.getClass());
- }
- }
- public int compareTo(Object o) {
- checkClass(o.getClass());
- return ((Enumerate) o).getOrdinal() - getOrdinal();
- }
- @Override
- public boolean equals(Object o) {
- if(null == o){
- return false;
- }
- if(o instanceof String)
- return getName().equals(o);
- if(o.getClass().equals(getClass()))
- return equals(((Enumerate)o).getOrdinal());
- return super.equals(o);
- }
- public boolean equals(int value) {
- return getOrdinal() == value;
- }
- public String getName() {
- return m_name;
- }
- public byte getOrdinal() {
- return (byte)m_ordinal;
- }
- private static Enumerate getNullValue(Class theClass) throws NullPointerException
- {
- // Cerca il campo marcato con il nome di NULLO
- Field fields[] = theClass.getFields();
- for (int i = 0; i < fields.length; i++)
- {
- Field field = fields[i];
- if(field.getName().equalsIgnoreCase("NULLO"))
- {
- try
- {
- return (Enumerate)field.get(null);
- }
- catch (IllegalArgumentException e) {}
- catch (IllegalAccessException e) {}
- }
- }
- throw new NullPointerException("Object NULLO not found in class " + theClass.getName());
- }
- /**
- * @param nclass La classe enumerata su cui fare la ricerca
- * @param b Il valore dell'ordinale da ricercare
- * @return Il valore dell'enumerato,null se non esiste
- */
- public static Enumerate getValue(Class nclass, int b)
- {
- try {
- Method metodo = nclass.getMethod("values", (Class[])null);
- Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null);
- for (int i = 0; i < enums.length; i++) {
- if (enums[i].getOrdinal() == b) {
- return enums[i];
- }
- }
- return getNullValue(nclass);
- } catch (SecurityException e) {
- throw new InvalidEnumerateException(nclass);
- } catch (IllegalArgumentException e) {
- throw new InvalidEnumerateException(nclass);
- } catch (NoSuchMethodException e) {
- throw new InvalidEnumerateException(nclass);
- } catch (IllegalAccessException e) {
- throw new InvalidEnumerateException(nclass);
- } catch (InvocationTargetException e) {
- throw new InvalidEnumerateException(nclass);
- }
- }
- /**
- * @param nclass La classe enumerata su cui fare la ricerca
- * @param b Il valore testuale su cui fare la ricerca;se ? un valore numerico in formato
- * stringa allora viene usata la versione di ricerca sull'ordinale,altrimenti quella
- * di ricerca sul nome
- * @return Il valore dell'enumerato,null se non esiste
- */
- public static Enumerate getValue(Class theClass,String value)
- {
- try
- {
- // Se la stringa ? un numero allora utilizza la versione che ricerca l'ordinale
- return getValue(theClass,Integer.parseInt(value));
- }
- catch(NumberFormatException nfe)
- {
- }
- try
- {
- Method metodo = theClass.getMethod("values", (Class[])null);
- Enumerate[] enums = (Enumerate[]) metodo.invoke(null, (Object[])null);
- for (int i = 0; i < enums.length; i++) {
- if (enums[i].getName().equals(value)) {
- return enums[i];
- }
- }
- return getNullValue(theClass);
- } catch (SecurityException e) {
- throw new InvalidEnumerateException(theClass);
- } catch (IllegalArgumentException e) {
- throw new InvalidEnumerateException(theClass);
- } catch (NoSuchMethodException e) {
- throw new InvalidEnumerateException(theClass);
- } catch (IllegalAccessException e) {
- throw new InvalidEnumerateException(theClass);
- } catch (InvocationTargetException e) {
- throw new InvalidEnumerateException(theClass);
- }
- }
- @Override
- public String toString(){
- return String.valueOf(getOrdinal());
- }
- @Override
- public int hashCode()
- {
- final int PRIME = 31;
- int result = 1;
- result = PRIME * result + new Integer(m_ordinal).hashCode();
- return result;
- }
- }
|