View Javadoc

1   package uk.ac.ebi.intenz.domain.constants;
2   
3   import java.util.Arrays;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import uk.ac.ebi.biobabel.util.collections.OperatorSet;
8   import uk.ac.ebi.intenz.domain.enzyme.Viewable;
9   
10  
11  /**
12   * This class represents an enumeration of all view codes.
13   * <p/>
14   * Instances of this class are immutable.
15   *
16   * @author Michael Darsow
17   * @version $Revision: 1.4 $ $Date: 2009/04/16 15:01:04 $
18   * @deprecated use enumeration {@link View} instead.
19   */
20  public class EnzymeViewConstant {
21    private final String viewCode;
22  
23    public static final EnzymeViewConstant UNDEF = new EnzymeViewConstant("UNDEF");
24    public static final EnzymeViewConstant IUBMB = new EnzymeViewConstant("IUBMB");
25    public static final EnzymeViewConstant SIB = new EnzymeViewConstant("SIB");
26    public static final EnzymeViewConstant IUBMB_SIB = new EnzymeViewConstant("IUBMB_SIB");
27    public static final EnzymeViewConstant IUBMB_INTENZ = new EnzymeViewConstant("IUBMB_INTENZ");
28    public static final EnzymeViewConstant SIB_INTENZ = new EnzymeViewConstant("SIB_INTENZ");
29    public static final EnzymeViewConstant INTENZ = new EnzymeViewConstant("INTENZ");
30  
31    private static final EnzymeViewConstant[] PRIVATE_ARRAY = {IUBMB, SIB, IUBMB_SIB, IUBMB_INTENZ, SIB_INTENZ, INTENZ};
32    public static final List<EnzymeViewConstant> VIEW_CONSTANTS =
33  	  Collections.unmodifiableList(Arrays.asList(PRIVATE_ARRAY));
34  
35    /**
36     * Object cannot be created outside this class.
37     *
38     * @param viewCode The line type code.
39     */
40    private EnzymeViewConstant(String viewCode) {
41      this.viewCode = viewCode;
42    }
43  
44    /**
45     * Returns the corresponding instance of the given view code.
46     * <p/>
47     * If the view code does not match any code an exception is thrown.
48     *
49     * @param viewCode The view code.
50     * @return the class constant corresponding to the given code.
51     * @throws NullPointerException     if <code>viewCode</code> is <code>null</code>.
52     * @throws IllegalArgumentException if the code is invalid.
53     */
54    public static EnzymeViewConstant valueOf(String viewCode) {
55      if (viewCode == null) throw new NullPointerException("Parameter 'viewCode' must not be null.");
56      if (viewCode.equals(IUBMB.toString())) return IUBMB;
57      if (viewCode.equals(SIB.toString())) return SIB;
58      if (viewCode.equals(IUBMB_SIB.toString())) return IUBMB_SIB;
59      if (viewCode.equals(IUBMB_INTENZ.toString())) return IUBMB_INTENZ;
60      if (viewCode.equals(SIB_INTENZ.toString())) return SIB_INTENZ;
61      if (viewCode.equals(INTENZ.toString())) return INTENZ;
62  	throw new IllegalArgumentException("The given view code is not supported.");
63    }
64  
65    public boolean isInSIBView() {
66      return this == SIB || this == IUBMB_SIB || this == SIB_INTENZ || this == INTENZ;
67    }
68  
69    public boolean isInIUBMBView() {
70      return this == IUBMB || this == IUBMB_SIB || this == IUBMB_INTENZ || this == INTENZ;
71    }
72  
73    public boolean isInIntEnzView() {
74      return this == INTENZ || this == IUBMB_INTENZ || this == SIB_INTENZ;
75    }
76  
77    public static boolean isInSIBView(String viewCode) {
78      return viewCode.equals(SIB.viewCode) || viewCode.equals(IUBMB_SIB.viewCode) ||
79             viewCode.equals(SIB_INTENZ.viewCode) || viewCode.equals(INTENZ.viewCode);
80    }
81  
82    public static boolean isInIUBMBView(String viewCode) {
83      return viewCode.equals(IUBMB.viewCode) || viewCode.equals(IUBMB_SIB.viewCode) ||
84             viewCode.equals(IUBMB_INTENZ.viewCode) || viewCode.equals(INTENZ.viewCode);
85    }
86  
87    public static boolean isInIntEnzView(String viewCode) {
88      return viewCode.equals(INTENZ.viewCode) || viewCode.equals(IUBMB_INTENZ.viewCode) ||
89             viewCode.equals(SIB_INTENZ.viewCode);
90    }
91    
92    public static boolean isInView(EnzymeViewConstant theView, Object o){
93  	  EnzymeViewConstant view = null;
94  	  if (o instanceof Viewable){
95  		  view = ((Viewable) o).getView();
96  	  } else if (o instanceof OperatorSet){
97  		  // Take first one to extract view info, as all of them must be the same:
98    		Viewable v = null;
99  		OperatorSet loopOs = (OperatorSet) o;
100 		while (v == null){
101 			Object obj = loopOs.iterator().next();
102 			if (obj instanceof Viewable) v = (Viewable) obj;
103 			else loopOs = (OperatorSet) obj;
104 		}
105 		view = v.getView();
106 	  }
107 	  return view.toString().contains(theView.toString()) || EnzymeViewConstant.INTENZ.equals(view);
108   }
109 
110   /**
111    * Standard equals method.
112    *
113    * @param o Object to be compared to this one.
114    * @return <code>true</code> if the objects are equal.
115    */
116     @Override
117   public boolean equals(Object o) {
118     if (this == o) return true;
119     if (!(o instanceof EnzymeViewConstant)) return false;
120 
121     final EnzymeViewConstant enzymeSource = (EnzymeViewConstant) o;
122 
123     if (!viewCode.equals(enzymeSource.viewCode)) return false;
124 
125     return true;
126   }
127 
128   /**
129    * Returns the hash code of this object.
130    *
131    * @return the hash code of this object.
132    */
133     @Override
134   public int hashCode() {
135     return viewCode.hashCode();
136   }
137 
138   /**
139    * Returns the database code.
140    *
141    * @return the database code.
142    */
143     @Override
144   public String toString() {
145     return viewCode;
146   }
147 
148   /**
149    * @deprecated
150    * @param view
151    * @return
152    */
153   public static String toDisplayString(String view) {
154     EnzymeViewConstant viewConstant = EnzymeViewConstant.valueOf(view);
155     if(viewConstant == UNDEF) return "undefined";
156     if(viewConstant == IUBMB) return "NC-IUBMB";
157     if(viewConstant == SIB) return "ENZYME";
158     if(viewConstant == INTENZ) return "all views";
159     if(viewConstant == IUBMB_SIB) return "NC-IUBMB & ENZYME";
160     if(viewConstant == IUBMB_INTENZ) return "NC-IUBMB & IntEnz";
161     return "ENZYME & IntEnz";
162   }
163 
164   /**
165    * @deprecated
166    * @return
167    */
168   public String toDisplayString() {
169     if(this == UNDEF) return "undefined";
170     if(this == IUBMB) return "NC-IUBMB";
171     if(this == SIB) return "ENZYME";
172     if(this == INTENZ) return "all views";
173     if(this == IUBMB_SIB) return "NC-IUBMB & ENZYME";
174     if(this == IUBMB_INTENZ) return "NC-IUBMB & IntEnz";
175     return "ENZYME & IntEnz";
176   }
177 
178   /**
179    * @deprecated MVC sin
180    * @param view
181    * @return
182    */
183   public static String toDisplayImage(String view) {
184     EnzymeViewConstant viewConstant = EnzymeViewConstant.valueOf(view);
185     if(viewConstant == UNDEF) return "undefined";
186     if(viewConstant == IUBMB) return "<img src=\"images/green_bullet.gif\"/>";
187     if(viewConstant == SIB) return "<img src=\"images/red_bullet.gif\"/>";
188     if(viewConstant == INTENZ) return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/green_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
189     if(viewConstant == IUBMB_SIB) return "<img src=\"images/green_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
190     if(viewConstant == IUBMB_INTENZ) return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/green_bullet.gif\"/>";
191     return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
192   }
193 
194   /**
195    * @deprecated MVC sin
196    * @return
197    */
198   public String toDisplayImage() {
199     if(this == UNDEF) return "undefined";
200     if(this == IUBMB) return "<img src=\"images/green_bullet.gif\"/>";
201     if(this == SIB) return "<img src=\"images/red_bullet.gif\"/>";
202     if(this == INTENZ) return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/green_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
203     if(this == IUBMB_SIB) return "<img src=\"images/green_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
204     if(this == IUBMB_INTENZ) return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/green_bullet.gif\"/>";
205     return "<img src=\"images/blue_bullet.gif\"/><img src=\"images/red_bullet.gif\"/>";
206   }
207 }
208 
209