View Javadoc
1   
2   package eu.simuline.util;
3   
4   
5   /**
6    * Enumerates categories of data models 
7    * and determines the current data model. 
8    * This is needed for jne/jna-applications, 
9    * not when used with pure jave. 
10   */
11  public enum DataModel {
12   
13      /*----------------------------------------------------------------------*
14       * constants                                                            *
15       *----------------------------------------------------------------------*/
16  
17      /**
18       * Represents a data model known to be based on 64-bit entities. 
19       */
20      Bits64("64"),
21  
22      /**
23       * Represents a data model known to be based on 32-bit entities. 
24       */
25      Bits32("32"),
26  
27      /**
28       * Represents a data model with unknown number of bits or data model 
29       * representable neither by {@link #Bits64} nor by {@link #Bits32}. 
30       */
31      Unknown("unknown") {
32  	public boolean isKnown() {
33  	    return false;
34  	}
35      };
36  
37      /*----------------------------------------------------------------------*
38       * fields                                                               *
39       *----------------------------------------------------------------------*/
40  
41      private final String code;
42  
43      /*----------------------------------------------------------------------*
44       * constructors                                                         *
45       *----------------------------------------------------------------------*/
46  
47      DataModel(String code) {
48  	this.code = code;
49      }
50  
51      /*----------------------------------------------------------------------*
52       * methods                                                              *
53       *----------------------------------------------------------------------*/
54  
55      /**
56       * Returns the current data model. 
57       * If there is no valid data model, (not even {@link #Unknown}), 
58       * then <code>null</code> is returned if <code>retNull</code> is set. 
59       * Otherwise an exception is thrown. 
60       *
61       * @param retNull
62       *    whether <code>null</code> is returned if the data model is invalid. 
63       * @throws IllegalStateException
64       *    if the data model is unknown and <code>retNull</code> is not set. 
65       */
66      public static DataModel getDataModel(boolean retNull) {
67  	String code = System.getProperty("sun.arch.data.model");
68  	//DataModel dataModel = null;
69  	for (DataModel cand : DataModel.values()) {
70  	    if (cand.code.equals(code)) {
71  		return cand;
72  	    } // if 
73  	} // for 
74  	if (retNull) {
75  	    return null;
76  	}
77  	throw new IllegalStateException
78  	    ("Unknown data model: " + code + ". ");
79      }
80  
81      public String getCode() {
82  	return this.code;
83      }
84  
85      /**
86       * Returns true except for {@link #Unknown}. 
87       */
88      public boolean isKnown() {
89  	return true;
90      }
91  }