Skip to content
This repository
  • 10 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
44  C4Sci/src/main/java/c4sci/data/BooleanDataParameter.java
... ...
@@ -0,0 +1,44 @@
  1
+
  2
+package c4sci.data;
  3
+
  4
+import c4sci.data.internationalization.InternationalizableTerm;
  5
+
  6
+/**
  7
+ * @author jeanmarc.deniel
  8
+ *
  9
+ */
  10
+public class BooleanDataParameter extends DataParameter {
  11
+
  12
+	private boolean paramValue;
  13
+	
  14
+	/**
  15
+	 * Constructs a BooleanDataParaeter whose value is false 
  16
+	 */
  17
+	public BooleanDataParameter(String token_str,
  18
+			InternationalizableTerm name_term,
  19
+			InternationalizableTerm descr_term) {
  20
+		super(token_str, name_term, descr_term);
  21
+		paramValue = false;
  22
+	}
  23
+
  24
+
  25
+	@Override
  26
+	public String getParameterValue() {
  27
+		return Boolean.toString(paramValue);
  28
+	}
  29
+
  30
+
  31
+	@Override
  32
+	public void setParameterValue(String str_to_parse)
  33
+			throws DataValueParsingException {
  34
+		paramValue = Boolean.parseBoolean(str_to_parse);
  35
+	}
  36
+	
  37
+	public void setBooleanValue(final boolean b_val){
  38
+		paramValue = b_val;
  39
+	}
  40
+	public boolean getBooleanValue(){
  41
+		return paramValue;
  42
+	}
  43
+
  44
+}
34  C4Sci/src/main/java/c4sci/data/DataParameter.java
@@ -8,26 +8,46 @@
8 8
  * @author jeanmarc.deniel
9 9
  *
10 10
  */
11  
-public interface DataParameter {
12  
-	String getParameterValue();
  11
+public abstract class DataParameter {
  12
+	private String					paramToken;
  13
+	private InternationalizableTerm	paramName;
  14
+	private InternationalizableTerm	paramDescription;
  15
+
  16
+	public DataParameter(String token_str, InternationalizableTerm name_term, InternationalizableTerm descr_term){
  17
+		paramToken			= token_str;
  18
+		paramName			= name_term;
  19
+		paramDescription	= descr_term;
  20
+	}
13 21
 	/**
14 22
 	 * @return It should a string of type [a-z]([A-Z][a-z]*)*
15 23
 	 */
16  
-	String getParameterToken();
  24
+	public final String getParameterToken() {
  25
+		return paramToken;
  26
+	}
17 27
 	/**
18 28
 	 * The name to associate with the parameter in containers
19 29
 	 * @return
20 30
 	 */
21  
-	InternationalizableTerm getParameterName();
  31
+	public final InternationalizableTerm getParameterName() {
  32
+		return paramName;
  33
+	}
22 34
 	/**
23 35
 	 * 
24 36
 	 * @return A string describing the parameter
25 37
 	 */
26  
-	InternationalizableTerm getParameterDescription();
  38
+	public final InternationalizableTerm getParameterDescription() {
  39
+		return paramDescription;
  40
+	}
27 41
 	/**
  42
+	 * 
  43
+	 * @return A String that could be parsed to retrieve the real parameter value.
  44
+	 */
  45
+	public abstract String getParameterValue();
  46
+
  47
+	/** 
28 48
 	 * Parses a String to set the value.
29  
-	 * @throws DataValueParsingException
  49
+	 * @throws DataValueParsingException if str_to_parse is null or cannot be parsed successfully
30 50
 	 */
31  
-	void setParameterValue(String str_to_parse) throws DataValueParsingException;
  51
+	public abstract void setParameterValue(String str_to_parse) throws DataValueParsingException;
32 52
 
33 53
 }
4  C4Sci/src/main/java/c4sci/data/DataValueParsingException.java
@@ -11,8 +11,8 @@
11 11
 	@SuppressWarnings("unused")
12 12
 	private DataValueParsingException() {}
13 13
 	
14  
-	public DataValueParsingException(String expected_value, String string_to_parse, String err_msg){
15  
-		super(err_msg);
  14
+	public DataValueParsingException(String expected_value, String string_to_parse, String err_msg, Throwable exception_cause){
  15
+		super(err_msg, exception_cause);
16 16
 		expectedValueType 	= expected_value;
17 17
 		stringToParse		= string_to_parse;
18 18
 		
38  C4Sci/src/main/java/c4sci/data/FloatDataParameter.java
... ...
@@ -0,0 +1,38 @@
  1
+package c4sci.data;
  2
+
  3
+import c4sci.data.internationalization.InternationalizableTerm;
  4
+
  5
+public class FloatDataParameter extends DataParameter {
  6
+
  7
+	private float paramValue;
  8
+	
  9
+	public FloatDataParameter(String token_str, InternationalizableTerm name_term, InternationalizableTerm descr_term) {
  10
+		super(token_str, name_term, descr_term);
  11
+		
  12
+	}
  13
+
  14
+	public String getParameterValue() {
  15
+		return Float.toString(paramValue);
  16
+	}
  17
+
  18
+	public void setParameterValue(String str_to_parse)
  19
+			throws DataValueParsingException {
  20
+		if (str_to_parse == null){
  21
+			throw new DataValueParsingException("float", "(null)", "parsing error : null parameter", null);
  22
+		}
  23
+		try{
  24
+		paramValue = Float.parseFloat(str_to_parse);
  25
+		}
  26
+		catch (NumberFormatException _e){
  27
+			throw new DataValueParsingException("float", str_to_parse, "parsing error", _e);
  28
+		}
  29
+
  30
+	}
  31
+
  32
+	public float getFloatValue(){
  33
+		return paramValue;
  34
+	}
  35
+	public void setFloatValue(final float fl_val){
  36
+		paramValue = fl_val;
  37
+	}
  38
+}
42  C4Sci/src/main/java/c4sci/data/IntegerDataParameter.java
... ...
@@ -0,0 +1,42 @@
  1
+package c4sci.data;
  2
+
  3
+import c4sci.data.internationalization.InternationalizableTerm;
  4
+
  5
+/**
  6
+ * This class is provided to contain and integer type parameter.
  7
+ * @author jeanmarc.deniel
  8
+ *
  9
+ */
  10
+public class IntegerDataParameter extends DataParameter {
  11
+
  12
+	private int paramValue;
  13
+	
  14
+	public IntegerDataParameter(String token_str, InternationalizableTerm name_term, InternationalizableTerm descr_term){
  15
+		super(token_str, name_term, descr_term);
  16
+		paramValue			= 0;
  17
+	}
  18
+	
  19
+	public String getParameterValue() {
  20
+		return Integer.toString(paramValue);
  21
+	}
  22
+
  23
+	public void setParameterValue(String str_to_parse)
  24
+			throws DataValueParsingException {
  25
+		if (str_to_parse == null){
  26
+			throw new DataValueParsingException("Integer", "(null)", "parsing error : null argument", null);
  27
+		}
  28
+		try{
  29
+		paramValue = Integer.parseInt(str_to_parse);
  30
+		}
  31
+		catch (NumberFormatException _e){
  32
+			throw new DataValueParsingException("Integer value", str_to_parse, "Integer parsing error", _e);
  33
+		}
  34
+	}
  35
+
  36
+	public int getIntegerValue(){
  37
+		return paramValue;
  38
+	}
  39
+	public void setIntegerValue(final int int_val){
  40
+		paramValue = int_val;
  41
+	}
  42
+}
43  C4Sci/src/test/java/c4sci/data/TestBooleanParameter.java
... ...
@@ -0,0 +1,43 @@
  1
+package c4sci.data;
  2
+
  3
+import static org.junit.Assert.*;
  4
+
  5
+import org.junit.Test;
  6
+
  7
+import c4sci.data.internationalization.InternationalizableTerm;
  8
+
  9
+public class TestBooleanParameter {
  10
+
  11
+	@Test
  12
+	public void test() {
  13
+		BooleanDataParameter _param = new BooleanDataParameter("param", 
  14
+				new InternationalizableTerm("boolean param"), 
  15
+				new InternationalizableTerm("param descr"));
  16
+		
  17
+		try {
  18
+			_param.setParameterValue("true");
  19
+		} catch (DataValueParsingException e) {
  20
+			fail();
  21
+		}
  22
+		assertTrue(_param.getBooleanValue());
  23
+		
  24
+		try {
  25
+			_param.setParameterValue("kj");
  26
+		} catch (DataValueParsingException e) {
  27
+			fail();
  28
+		}
  29
+		assertFalse(_param.getBooleanValue());
  30
+		
  31
+		_param.setBooleanValue(true);
  32
+		try {
  33
+			_param.setParameterValue(null);
  34
+		} catch (DataValueParsingException e) {
  35
+			fail();
  36
+		}
  37
+		assertFalse(_param.getBooleanValue());
  38
+		
  39
+		assertTrue("false".compareTo(_param.getParameterValue())==0);
  40
+		
  41
+	}
  42
+
  43
+}
60  C4Sci/src/test/java/c4sci/data/TestFloatDataParameter.java
... ...
@@ -0,0 +1,60 @@
  1
+package c4sci.data;
  2
+
  3
+import static org.junit.Assert.*;
  4
+
  5
+import org.junit.Test;
  6
+
  7
+import c4sci.data.internationalization.InternationalizableTerm;
  8
+
  9
+public class TestFloatDataParameter {
  10
+
  11
+	@Test
  12
+	public void testGetParameterValue() {
  13
+		DataParameter _param = new FloatDataParameter("test1", 
  14
+				new InternationalizableTerm("test param 1"), 
  15
+				new InternationalizableTerm("test param 1 descr"));
  16
+		try {
  17
+			_param.setParameterValue("0.5");
  18
+			_param.setParameterValue("10");
  19
+			assertTrue(true);
  20
+		} catch (DataValueParsingException e) {
  21
+			fail();
  22
+		}
  23
+		
  24
+		try {
  25
+			_param.setParameterValue("ae");
  26
+			fail();
  27
+		} catch (DataValueParsingException e) {
  28
+			assertTrue(true);
  29
+		}
  30
+		
  31
+		try {
  32
+			_param.setParameterValue(null);
  33
+			fail();
  34
+		} catch (DataValueParsingException e) {
  35
+			assertTrue(true);
  36
+		}
  37
+		
  38
+		assertTrue("10.0".compareTo(_param.getParameterValue())==0);
  39
+	}
  40
+
  41
+	@Test
  42
+	public void testFloatSetGet(){
  43
+		FloatDataParameter _param = new FloatDataParameter("test1", 
  44
+				new InternationalizableTerm("test param 1"), 
  45
+				new InternationalizableTerm("test param 1 descr"));
  46
+		
  47
+		try {
  48
+			_param.setParameterValue("10.5");
  49
+			assertTrue(true);
  50
+		} catch (DataValueParsingException e) {
  51
+			fail();
  52
+		}
  53
+		
  54
+		assertEquals(_param.getFloatValue(), 10.5, .01);
  55
+		
  56
+		_param.setFloatValue(2.5f);
  57
+		assertEquals(_param.getFloatValue(), 2.5, .01);
  58
+	}
  59
+
  60
+}
139  C4Sci/src/test/java/c4sci/data/TestHierarchicalData.java
@@ -3,18 +3,15 @@
3 3
 import static org.junit.Assert.*;
4 4
 
5 5
 import org.junit.Test;
6  
-import org.omg.PortableInterceptor.SUCCESSFUL;
7  
-
8 6
 import c4sci.data.internationalization.InternationalizableTerm;
9 7
 
10 8
 public class TestHierarchicalData {
11 9
 
12 10
 	@Test
13 11
 	public void testHierarchicalData() {
14  
-		HierarchicalData _data = new HierarchicalData("test_data", 
  12
+		assertTrue(new HierarchicalData("test_data", 
15 13
 				new InternationalizableTerm("testing data"), 
16  
-				new InternationalizableTerm("this data is used to test the HierarchicalData class"));
17  
-		assertTrue(true);
  14
+				new InternationalizableTerm("this data is used to test the HierarchicalData class")) != null);
18 15
 	}
19 16
 
20 17
 	@Test
@@ -42,41 +39,36 @@ public void testGetDataDescription() {
42 39
 		assertTrue("this data is used to test the HierarchicalData class".compareTo(_data.getDataDescription().getDefaultValue())==0);
43 40
 	}
44 41
 
  42
+	class TestDataParameter  extends DataParameter{
  43
+		public TestDataParameter(String a_, InternationalizableTerm b_, InternationalizableTerm c_){
  44
+			super(a_,b_,c_);
  45
+			paramValue = 0;
  46
+		}
  47
+		private int	paramValue;
  48
+		public void setParameterValue(String str_to_parse)
  49
+				throws DataValueParsingException {
  50
+			try{
  51
+				paramValue = Integer.parseInt(str_to_parse);
  52
+			}
  53
+			catch(NumberFormatException _e){
  54
+				throw new DataValueParsingException("integer", str_to_parse, "integer parsing error", _e);
  55
+			}
  56
+			
  57
+		}
  58
+		
  59
+		public String getParameterValue() {
  60
+			return Integer.toString(paramValue);
  61
+		}
  62
+
  63
+	};
45 64
 	@Test
46 65
 	public void testAddDataParameter() {
47 66
 		HierarchicalData _data = new HierarchicalData("test_data", 
48 67
 				new InternationalizableTerm("testing data"), 
49 68
 				new InternationalizableTerm("this data is used to test the HierarchicalData class"));
50  
-		DataParameter _param = new DataParameter() {
51  
-			private int	paramValue;
52  
-			public void setParameterValue(String str_to_parse)
53  
-					throws DataValueParsingException {
54  
-				try{
55  
-					paramValue = Integer.parseInt(str_to_parse);
56  
-				}
57  
-				catch(NumberFormatException _e){
58  
-					throw new DataValueParsingException("integer", str_to_parse, "integer parsing error");
59  
-				}
60  
-				
61  
-			}
62  
-			
63  
-			public String getParameterValue() {
64  
-				return Integer.toString(paramValue);
65  
-			}
66  
-			
67  
-			public String getParameterToken() {
68  
-				return "testParam";
69  
-			}
70  
-			
71  
-			public InternationalizableTerm getParameterName() {
72  
-				return new InternationalizableTerm("testing parameter");
73  
-			}
74  
-			
75  
-			public InternationalizableTerm getParameterDescription() {
76  
-				return new InternationalizableTerm("An integer testing parameter");
77  
-			}
78  
-
79  
-		};
  69
+		DataParameter _param = new TestDataParameter("int", 
  70
+				new InternationalizableTerm("integer param"), 
  71
+				new InternationalizableTerm("testing int param"));
80 72
 
81 73
 		_data.addDataParameter(_param);
82 74
 		assertTrue(true);
@@ -88,35 +80,9 @@ public void testSetGetParameterValue() {
88 80
 		HierarchicalData _data = new HierarchicalData("test_data", 
89 81
 				new InternationalizableTerm("testing data"), 
90 82
 				new InternationalizableTerm("this data is used to test the HierarchicalData class"));
91  
-		DataParameter _param = new DataParameter() {
92  
-			private int	paramValue;
93  
-			public void setParameterValue(String str_to_parse)
94  
-					throws DataValueParsingException {
95  
-				try{
96  
-					paramValue = Integer.parseInt(str_to_parse);
97  
-				}
98  
-				catch(NumberFormatException _e){
99  
-					throw new DataValueParsingException("integer", str_to_parse, "integer parsing error");
100  
-				}
101  
-				
102  
-			}
103  
-			
104  
-			public String getParameterValue() {
105  
-				return Integer.toString(paramValue);
106  
-			}
107  
-			
108  
-			public String getParameterToken() {
109  
-				return "testParam";
110  
-			}
111  
-			
112  
-			public InternationalizableTerm getParameterName() {
113  
-				return new InternationalizableTerm("testing parameter");
114  
-			}
115  
-			
116  
-			public InternationalizableTerm getParameterDescription() {
117  
-				return new InternationalizableTerm("An integer testing parameter");
118  
-			}
119  
-		};
  83
+		DataParameter _param = new TestDataParameter("testParam", 
  84
+				new InternationalizableTerm("integer param"), 
  85
+				new InternationalizableTerm("testing int param"));
120 86
 
121 87
 		_data.addDataParameter(_param);
122 88
 		
@@ -170,44 +136,13 @@ public void testAcceptVisitor() {
170 136
 				new InternationalizableTerm("Second child"), 
171 137
 				new InternationalizableTerm("2nd child description"));
172 138
 		
173  
-		class TestDataParameter implements DataParameter{
174  
-			
175  
-			private int	paramValue;
176  
-			String 		tokenValue;
177  
-			
178  
-			public TestDataParameter(String token_str){
179  
-				tokenValue = token_str;
180  
-			}
181  
-			
182  
-			public void setParameterValue(String str_to_parse)
183  
-					throws DataValueParsingException {
184  
-				try{
185  
-					paramValue = Integer.parseInt(str_to_parse);
186  
-				}
187  
-				catch(NumberFormatException _e){
188  
-					throw new DataValueParsingException("integer", str_to_parse, "integer parsing error");
189  
-				}
190  
-				
191  
-			}
192  
-			
193  
-			public String getParameterValue() {
194  
-				return Integer.toString(paramValue);
195  
-			}
196  
-			
197  
-			public String getParameterToken() {
198  
-				return tokenValue;
199  
-			}
200  
-			
201  
-			public InternationalizableTerm getParameterName() {
202  
-				return new InternationalizableTerm("testing parameter");
203  
-			}
204  
-			
205  
-			public InternationalizableTerm getParameterDescription() {
206  
-				return new InternationalizableTerm("An integer testing parameter");
207  
-			}
208  
-		}
209  
-		DataParameter _param = new TestDataParameter("param1") ;
210  
-		DataParameter _param_2 = new TestDataParameter("param2");
  139
+		
  140
+		DataParameter _param = new TestDataParameter("param1", 
  141
+				new InternationalizableTerm("testparam1"),
  142
+				new InternationalizableTerm("descr 1")) ;
  143
+		DataParameter _param_2 = new TestDataParameter("param2",
  144
+				new InternationalizableTerm("testparam2"),
  145
+				new InternationalizableTerm("descr 2"));
211 146
 
212 147
 		_data.addDataParameter(_param);
213 148
 		_data.addDataParameter(_param);
52  C4Sci/src/test/java/c4sci/data/TestIntegerDataParameter.java
... ...
@@ -0,0 +1,52 @@
  1
+package c4sci.data;
  2
+
  3
+import static org.junit.Assert.*;
  4
+
  5
+import org.junit.Test;
  6
+
  7
+import c4sci.data.internationalization.InternationalizableTerm;
  8
+
  9
+public class TestIntegerDataParameter {
  10
+
  11
+	@Test
  12
+	public void test() {
  13
+		IntegerDataParameter _param = new IntegerDataParameter("testParam", 
  14
+				new InternationalizableTerm("testing parameter"), 
  15
+				new InternationalizableTerm("integer testing parameter"));
  16
+		
  17
+		assertTrue("0".compareTo(_param.getParameterValue())==0);
  18
+		assertTrue("testParam".compareTo(_param.getParameterToken())==0);
  19
+		assertTrue("testing parameter".compareTo(_param.getParameterName().getDefaultValue())==0);
  20
+		assertTrue("integer testing parameter".compareTo(_param.getParameterDescription().getDefaultValue())==0);
  21
+		
  22
+		try {
  23
+			_param.setParameterValue("20");
  24
+			assertTrue(true);
  25
+		} catch (DataValueParsingException _e) {
  26
+			fail(_e.getStringToParse());
  27
+		}
  28
+		
  29
+		try{
  30
+			_param.setParameterValue("a25");
  31
+			fail("should have thrown an exception");
  32
+		}
  33
+		catch(DataValueParsingException _e){
  34
+			assertTrue(true);
  35
+		}
  36
+	
  37
+		try{
  38
+			_param.setParameterValue(null);
  39
+			fail("should have thrown an exception");
  40
+		}
  41
+		catch(DataValueParsingException _e){
  42
+			assertTrue(true);
  43
+		}
  44
+		assertTrue("20".compareTo(_param.getParameterValue())==0);
  45
+		
  46
+		assertEquals(_param.getIntegerValue(), 20);
  47
+		
  48
+		_param.setIntegerValue(3);
  49
+		assertEquals(_param.getIntegerValue(), 3);
  50
+	}
  51
+
  52
+}

No commit comments for this range

Something went wrong with that request. Please try again.