View Javadoc

1   package org.diyefi.openlogviewer.decoder;
2   
3   public class LogField {
4   	public enum types {
5   		UINT8  (1),
6   		UINT16 (2),
7   		UINT32 (4),
8   		BITS8  (1),
9   		BITS16 (2),
10  		BITS32 (4),
11  		SINT8  (1),
12  		SINT16 (2),
13  		SINT32 (4);
14  
15  		private final int width;
16  
17  		private types(final int width) {
18  			this.width = width;
19  		}
20  
21  		public final int getWidth() {
22  			return width;
23  		}
24  	}
25  
26  	static final double divideByOne = 1.0;
27  	static final double addToZero = 0.0;
28  
29  	private String ID;
30  	private types type;
31  	private String[] bitFieldNames;
32  
33  	// Optional
34  	private String description;
35  	private String unit;
36  
37  	// This does not match MTX:
38  	private double divBy = divideByOne; // divide first (mtx multiplies, and the result is uglier config IMO
39  	private double addTo = addToZero;   // add second
40  
41  	public LogField(final String ID) {
42  		this(ID, types.UINT16, divideByOne, addToZero);
43  	}
44  	public LogField(final String ID, final types type) {
45  		this(ID, type, divideByOne, addToZero);
46  	}
47  	public LogField(final String ID, final types type, final double divBy) {
48  		this(ID, type , divBy, addToZero);
49  	}
50  
51  	public LogField(final String ID, final double divBy) {
52  		this(ID, types.UINT16, divBy, addToZero);
53  	}
54  	public LogField(final String ID, final double divBy, final double addTo) {
55  		this(ID, types.UINT16, divBy, addTo);
56  	}
57  
58  	public LogField(final String ID, final types type, final double divBy, final double addTo) {
59  		this.ID = ID;
60  		this.type = type;
61  		this.divBy = divBy;
62  		this.addTo = addTo;
63  
64  		if (ID == null) {
65  			throw new RuntimeException("ID is null, needs to be a valid string!");
66  		} else if (type == null) {
67  			throw new RuntimeException("Type must be specified!");
68  		} else if ((this.type == types.BITS8) || (this.type == types.BITS16) || (this.type == types.BITS32)) {
69  			throw new RuntimeException("This constructor must NOT be used with flag variables");
70  		} else if (this.divBy == addToZero) {
71  			throw new RuntimeException("Divide by zero not possible, don't be silly!");
72  		}
73  	}
74  
75  	public LogField(final String ID, final types type, final String[] bitFieldNames) {
76  		this.ID = ID;
77  		this.type = type;
78  		this.bitFieldNames = bitFieldNames;
79  
80  		if (ID == null) {
81  			throw new RuntimeException("ID is null, needs to be a valid string!");
82  		} else if (this.type != types.BITS8 && this.type != types.BITS16 && this.type != types.BITS32) {
83  			throw new RuntimeException("This constructor can only be used with flag variables!");
84  		}
85  
86  		if (this.bitFieldNames == null) {
87  			throw new IllegalArgumentException("bitFieldNames is null!");
88  		} else if ((this.type == types.BITS8) && (bitFieldNames.length != 8)) {
89  			throw new IllegalArgumentException("BITS8 requires 8 flag names!");
90  		} else if ((this.type == types.BITS16) && (bitFieldNames.length != 16)) {
91  			throw new IllegalArgumentException("BITS16 requires 16 flag names!");
92  		} else if ((this.type == types.BITS32) && (bitFieldNames.length != 32)) {
93  			throw new IllegalArgumentException("BITS16 requires 32 flag names!");
94  		}
95  	}
96  
97  	public final String getID() {
98  		return ID;
99  	}
100 	public final types getType() {
101 		return type;
102 	}
103 	public final String[] getBitFieldNames() {
104 		return bitFieldNames;
105 	}
106 	public final double getDivBy() {
107 		return divBy;
108 	}
109 	public final double getAddTo() {
110 		return addTo;
111 	}
112 	public final void setID(final String iD) {
113 		ID = iD;
114 	}
115 	public final void setType(final types type) {
116 		this.type = type;
117 	}
118 	public final void setBitFieldNames(final String[] bitFieldNames) {
119 		this.bitFieldNames = bitFieldNames;
120 	}
121 	public final void setDivBy(final double divBy) {
122 		this.divBy = divBy;
123 	}
124 	public final void setAddTo(final double addTo) {
125 		this.addTo = addTo;
126 	}
127 	public final String getDescription() {
128 		return description;
129 	}
130 	public final String getUnit() {
131 		return unit;
132 	}
133 	public final void setDescription(final String description) {
134 		this.description = description;
135 	}
136 	public final void setUnit(final String unit) {
137 		this.unit = unit;
138 	}
139 }