Changeset 163
- Timestamp:
- 01/16/10 11:34:06 (2 years ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
build.xml (modified) (1 diff)
-
CHANGELOG (modified) (1 diff)
-
src-test/org/jergometer/model/BikeProgramDataTest.java (modified) (1 diff)
-
src/org/jergometer/model/BikeProgramData.java (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/build.xml
r161 r163 136 136 137 137 <!-- Validates the source code and creates a new release on the remote server. --> 138 <target name="create_remote_release" depends="compile ">138 <target name="create_remote_release" depends="compile,create_linux_source_package"> 139 139 <exec executable="scripts/create_remote_release"> 140 140 <arg value="${ant.project.name}-${program.version}.${build.number}"/> -
trunk/CHANGELOG
r161 r163 2 2 """""""""""""""""""""""" 3 3 4 //== 0.7.4 == 4 //== 0.7.5 == 5 6 2010-01-16 7 * [stefan] * implemented feature #164: allow relative values (with prefix "+") in training programs (for time, pulse and power) 8 9 == 0.7.4 == 5 10 6 11 2010-01-16 -
trunk/src-test/org/jergometer/model/BikeProgramDataTest.java
r73 r163 8 8 public class BikeProgramDataTest extends TestCase { 9 9 public void testTimeDefinitions() { 10 assertEquals(60, BikeProgramData. getTime("60"));11 assertEquals(60, BikeProgramData. getTime("60s"));12 assertEquals(60, BikeProgramData. getTime("1m"));13 assertEquals(60, BikeProgramData. getTime("1'"));14 assertEquals(60, BikeProgramData. getTime("1:0"));15 assertEquals(60, BikeProgramData. getTime("1:00"));16 assertEquals(60, BikeProgramData. getTime("1.0m"));17 assertEquals(60, BikeProgramData. getTime("1,0m"));18 assertEquals(90, BikeProgramData. getTime("1,5m"));19 assertEquals(90, BikeProgramData. getTime("1m30"));20 assertEquals(90, BikeProgramData. getTime("1m30s"));21 assertEquals(90, BikeProgramData. getTime("01:30"));22 assertEquals(90, BikeProgramData. getTime("0:01:30"));23 assertEquals(90, BikeProgramData. getTime("0h01m30"));24 assertEquals(3661, BikeProgramData. getTime("1:1:1"));25 assertEquals(3661, BikeProgramData. getTime("1h1m1"));10 assertEquals(60, BikeProgramData.GetTime.inst.apply("60")); 11 assertEquals(60, BikeProgramData.GetTime.inst.apply("60s")); 12 assertEquals(60, BikeProgramData.GetTime.inst.apply("1m")); 13 assertEquals(60, BikeProgramData.GetTime.inst.apply("1'")); 14 assertEquals(60, BikeProgramData.GetTime.inst.apply("1:0")); 15 assertEquals(60, BikeProgramData.GetTime.inst.apply("1:00")); 16 assertEquals(60, BikeProgramData.GetTime.inst.apply("1.0m")); 17 assertEquals(60, BikeProgramData.GetTime.inst.apply("1,0m")); 18 assertEquals(90, BikeProgramData.GetTime.inst.apply("1,5m")); 19 assertEquals(90, BikeProgramData.GetTime.inst.apply("1m30")); 20 assertEquals(90, BikeProgramData.GetTime.inst.apply("1m30s")); 21 assertEquals(90, BikeProgramData.GetTime.inst.apply("01:30")); 22 assertEquals(90, BikeProgramData.GetTime.inst.apply("0:01:30")); 23 assertEquals(90, BikeProgramData.GetTime.inst.apply("0h01m30")); 24 assertEquals(3661, BikeProgramData.GetTime.inst.apply("1:1:1")); 25 assertEquals(3661, BikeProgramData.GetTime.inst.apply("1h1m1")); 26 26 } 27 27 } -
trunk/src/org/jergometer/model/BikeProgramData.java
r76 r163 21 21 public void fromXML(XMLElement rootElement) { 22 22 name = rootElement.getAttribute("name"); 23 duration = getTime(rootElement.getAttribute("duration")); 23 duration = getValue(GetTime.inst, rootElement.getAttribute("duration"), 0); 24 25 LastState lastState = new LastState(); 24 26 25 27 XMLElement timeEventsElement = rootElement.getChildElement("timeevents"); … … 27 29 for (XMLElement timeEventElement : timeEventsElement.getChildElements()) { 28 30 if (timeEventElement.getName().equals("timeevent")) { 29 events.add(new TimeEvent(timeEventElement ));31 events.add(new TimeEvent(timeEventElement, lastState)); 30 32 } 31 33 } … … 51 53 } 52 54 53 public static int getTime(String timeString) { 54 timeString = timeString.replaceAll(",", "."); 55 String h = null, m = null, s = null; 56 if (timeString.indexOf(':') >= 0) { 57 String[] parts = timeString.split(":"); 58 switch (parts.length) { 59 case 3: 55 56 57 private static int getValue(IntFunction1<String> f, String string, int lastValue) { 58 int sign = 0; 59 60 if (string.startsWith("+")) { 61 sign = 1; 62 string = string.substring(1); 63 } else 64 if (string.startsWith("-")) { 65 sign = -1; 66 string = string.substring(1); 67 } 68 69 if (sign == 0) { 70 return f.apply(string); 71 } else { 72 return lastValue + sign * f.apply(string); 73 } 74 } 75 76 77 78 public static interface IntFunction1<T> { 79 public int apply(String s); 80 } 81 82 public static class GetTime implements IntFunction1<String> { 83 public static final GetTime inst = new GetTime(); 84 85 public int apply(String timeString) { 86 timeString = timeString.replaceAll(",", "."); 87 String h = null, m = null, s = null; 88 if (timeString.indexOf(':') >= 0) { 89 String[] parts = timeString.split(":"); 90 switch (parts.length) { 91 case 3: 92 h = parts[0]; 93 m = parts[1]; 94 s = parts[2]; 95 break; 96 case 2: 97 m = parts[0]; 98 s = parts[1]; 99 break; 100 case 1: 101 s = parts[0]; 102 } 103 } else { 104 if (timeString.indexOf('h') >= 0) { 105 String[] parts = timeString.split("h"); 60 106 h = parts[0]; 61 m = parts[1]; 62 s = parts[2]; 63 break; 64 case 2: 65 m = parts[0]; 66 s = parts[1]; 67 break; 68 case 1: 69 s = parts[0]; 70 } 71 } else { 72 if (timeString.indexOf('h') >= 0) { 73 String[] parts = timeString.split("h"); 74 h = parts[0]; 107 timeString = parts.length == 2 ? parts[1] : "0"; 108 } 109 if (timeString.indexOf('m') >= 0) { 110 String[] parts = timeString.split("m"); 111 m = parts[0]; 75 112 timeString = parts.length == 2 ? parts[1] : "0"; 76 } 77 if (timeString.indexOf('m') >= 0) { 78 String[] parts = timeString.split("m"); 79 m = parts[0]; 80 timeString = parts.length == 2 ? parts[1] : "0"; 81 } 82 if (timeString.indexOf('\'') >= 0) { 83 String[] parts = timeString.split("'"); 84 m = parts[0]; 85 timeString = parts.length == 2 ? parts[1] : "0"; 86 } 87 s = timeString.replace("s", ""); 88 } 89 113 } 114 if (timeString.indexOf('\'') >= 0) { 115 String[] parts = timeString.split("'"); 116 m = parts[0]; 117 timeString = parts.length == 2 ? parts[1] : "0"; 118 } 119 s = timeString.replace("s", ""); 120 } 121 122 int time = 0; 123 if (h != null) { 124 time += (int) (3600 * Double.parseDouble(h)); 125 } 126 if (m != null) { 127 time += (int) (60 * Double.parseDouble(m)); 128 } 129 if (s != null) { 130 time += Integer.parseInt(s); 131 } 132 133 return time; 134 } 135 } 136 public static class GetInt implements IntFunction1<String> { 137 public static final GetInt inst = new GetInt(); 138 139 public int apply(String s) { 140 return Integer.parseInt(s); 141 } 142 } 143 144 145 146 // inner classes 147 private class LastState { 90 148 int time = 0; 91 if (h != null) { 92 time += (int) (3600 * Double.parseDouble(h)); 93 } 94 if (m != null) { 95 time += (int) (60 * Double.parseDouble(m)); 96 } 97 if (s != null) { 98 time += Integer.parseInt(s); 99 } 100 101 return time; 102 } 103 104 105 // inner classes 149 int power = 0; 150 int pulse = 0; 151 152 public int getLast(Action.Type type) { 153 switch (type) { 154 case power: 155 return power; 156 case pulse: 157 return pulse; 158 } 159 return -1; 160 } 161 162 public void setLast(Action.Type type, int value) { 163 switch (type) { 164 case power: 165 power = value; 166 return; 167 case pulse: 168 pulse = value; 169 } 170 } 171 } 172 106 173 public static class TimeEvent { 107 174 protected int time; 108 175 protected ArrayList<Action> actions = new ArrayList<Action>(); 109 176 110 public TimeEvent(XMLElement element ) {111 fromXML(element );112 } 113 114 public void fromXML(XMLElement element ) {115 String timeString = element.getAttribute("time");116 time = BikeProgramData.getTime(timeString);177 public TimeEvent(XMLElement element, LastState lastState) { 178 fromXML(element, lastState); 179 } 180 181 public void fromXML(XMLElement element, LastState lastState) { 182 time = BikeProgramData.getValue(GetTime.inst, element.getAttribute("time"), lastState.time); 183 lastState.time = time; 117 184 118 185 for (XMLElement actionElement : element.getChildElements()) { 119 186 if(actionElement.getName().equals("action")) { 120 actions.add(new Action(actionElement ));187 actions.add(new Action(actionElement, lastState)); 121 188 } 122 189 } … … 144 211 } 145 212 146 public Action(XMLElement element ) {147 fromXML(element );148 } 149 150 public void fromXML(XMLElement element ) {213 public Action(XMLElement element, LastState lastState) { 214 fromXML(element, lastState); 215 } 216 217 public void fromXML(XMLElement element, LastState lastState) { 151 218 type = Type.valueOf(element.getAttribute("type")); 152 value = Integer.parseInt(element.getAttribute("value")); 219 value = BikeProgramData.getValue(GetInt.inst, element.getAttribute("value"), lastState.getLast(type)); 220 lastState.setLast(type, value); 153 221 } 154 222
![(please configure the [header_logo] section in trac.ini)](https://jergometer.org/images/fork_me_on_github.png)