View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.structure;
22  
23  import java.io.InvalidObjectException;
24  import java.io.ObjectStreamException;
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /***
33   * 
34   * @author Fredrik Vraalsen
35   */
36  public final class ViewpointEnum implements Serializable {
37  	
38  	public static final ViewpointEnum ENTERPRISE = new ViewpointEnum("Enterprise");
39  	public static final ViewpointEnum INFORMATION = new ViewpointEnum("Information");
40  	public static final ViewpointEnum COMPUTATIONAL = new ViewpointEnum("Computational");
41  	public static final ViewpointEnum ENGINEERING = new ViewpointEnum("Engineering");
42  	public static final ViewpointEnum TECHNOLOGY = new ViewpointEnum("Technology");
43  	private static final ViewpointEnum[] VALS = { 
44  			ENTERPRISE, 
45  			INFORMATION, 
46  			COMPUTATIONAL, 
47  			ENGINEERING, 
48  			TECHNOLOGY };
49  	public static final List VALUES = Collections.unmodifiableList(Arrays.asList(VALS));
50  	private static final List NAMES = new ArrayList(); 
51  	static {
52  		for (Iterator i = VALUES.iterator(); i.hasNext();) {
53  			NAMES.add(i.next().toString());
54  		}
55  	}
56  	
57  	private final String name;
58  	
59  	/***
60  	 * Private constructor.
61  	 * 
62  	 * @param name
63  	 *            the name of the viewpoint
64  	 */
65  	private ViewpointEnum(String name) {
66  		this.name = name;
67  	}
68  
69  	/***
70  	 * Get the ViewpointEnum corresponding to the specified name.
71  	 * 
72  	 * @param name
73  	 *            the viewpoint name
74  	 * @return the ViewpointEnum, or null if not found
75  	 */
76  	public static ViewpointEnum forName(String name) {
77  		int index = NAMES.indexOf(name);
78  		if (index < 0 || index >= VALS.length) {
79  			return null;
80  		} else {
81  			return VALS[index];
82  		}
83  	}
84  	
85  	/***
86  	 * Get the String representation.
87  	 * 
88  	 * @return the name of the viewpoint
89  	 */
90  	public String toString() {
91  		return name;
92  	}
93  
94  	/***
95  	 * Enumeration pattern: Ensure that a.equals(b) <=> (a == b) for all
96  	 * ViewpointEnums a and b when deserializing.
97  	 * 
98  	 * @return the ViewpointEnum corresponding to the viewpoint string
99  	 * @throws ObjectStreamException
100 	 *             if no ViewpointEnum can be found for that viewpoint string
101 	 */
102 	private Object readResolve() throws ObjectStreamException {
103 		ViewpointEnum result = forName(name);
104 		if (result == null) {
105 			throw new InvalidObjectException("Illegal ViewpointEnum instance: " + name);
106 		}
107 		return result;
108 	}
109 
110 }