diff --git a/src/main/java/no/nibio/vips/entity/ResultImpl.java b/src/main/java/no/nibio/vips/entity/ResultImpl.java index 17f76f0651985efe5881a72601d1a1512286e727..f8da1f68b64c5afb24eaebf28c67ee13f87163f3 100644 --- a/src/main/java/no/nibio/vips/entity/ResultImpl.java +++ b/src/main/java/no/nibio/vips/entity/ResultImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 NIBIO <http://www.nibio.no/>. + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. * * This file is part of VIPSCommon. * VIPSCommon is free software: you can redistribute it and/or modify @@ -19,10 +19,13 @@ package no.nibio.vips.entity; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.TimeZone; /** * Represents a result @@ -52,7 +55,7 @@ public class ResultImpl implements Result{ @Override public Set<String> getKeys() { - return this.values.keySet(); + return this.values != null ? this.values.keySet() : new HashSet<String>(); } @Override @@ -69,7 +72,8 @@ public class ResultImpl implements Result{ @Override public Map<String, String> getAllValues() { - return this.values; + + return this.values != null ? this.values : new HashMap<String,String>(); } @Override @@ -97,4 +101,22 @@ public class ResultImpl implements Result{ return this.warningStatus != null ? this.warningStatus: WARNING_STATUS_NO_WARNING; } + @Override + public String toString(){ + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + String retVal = "[" + format.format(resultValidTime) + "], WARNING_STATUS=" + this.getWarningStatus() + ", "; + if(this.getKeys() != null) + { + for(String key: this.getKeys()) + { + String[] keyElements = key.split("\\."); + if(keyElements.length == 2) + { + retVal += key + "=" + this.getValue(keyElements[0], keyElements[1]) + ", "; + } + } + } + return retVal; + } } diff --git a/src/main/java/no/nibio/vips/util/TimePeriod.java b/src/main/java/no/nibio/vips/util/TimePeriod.java new file mode 100644 index 0000000000000000000000000000000000000000..8487c0ebd85ba0c00fbb30f17aca42fed14bb03d --- /dev/null +++ b/src/main/java/no/nibio/vips/util/TimePeriod.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.util; + +import java.util.Date; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class TimePeriod implements Comparable{ + private Date start, end; + + public TimePeriod() + { + + } + + public TimePeriod(Date start, Date end) + { + this.start = start; + this.end = end; + } + + public boolean isDateInTimePeriod(Date date) + { + return ! (date.before(this.start) || date.after(this.end)); + } + + public boolean isDateBeforeTimePeriod(Date date) + { + return date.before(this.start); + } + + public boolean isDateAfterTimePeriod(Date date) + { + return date.after(this.end); + } + + /** + * @return the start + */ + public Date getStart() { + return start; + } + + /** + * @param start the start to set + */ + public TimePeriod setStart(Date start) { + this.start = start; + return this; + } + + /** + * @return the end + */ + public Date getEnd() { + return end; + } + + /** + * @param end the end to set + */ + public TimePeriod setEnd(Date end) { + this.end = end; + return this; + } + + @Override + public int compareTo(Object t) { + TimePeriod other = (TimePeriod) t; + return this.getStart().compareTo(other.getStart()); + } + + + +} diff --git a/src/test/java/no/nibio/vips/util/TimePeriodTest.java b/src/test/java/no/nibio/vips/util/TimePeriodTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4cf132375f08311d3c22e5007f4ce01600924b04 --- /dev/null +++ b/src/test/java/no/nibio/vips/util/TimePeriodTest.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ +package no.nibio.vips.util; + +import java.util.Calendar; +import java.util.Date; +import junit.framework.TestCase; + +/** + * + * @author treinar + */ +public class TimePeriodTest extends TestCase { + + public TimePeriodTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + /** + * Test of isDateInTimePeriod method, of class TimePeriod. + */ + public void testIsDateInTimePeriod() { + System.out.println("isDateInTimePeriod"); + Calendar cal = Calendar.getInstance(); + cal.set(2015,Calendar.AUGUST,5,12,0); + cal.set(Calendar.MILLISECOND, 0); + Date date = cal.getTime(); + TimePeriod instance = new TimePeriod(); + instance.setStart(cal.getTime()); + cal.set(2015, Calendar.AUGUST,10,11,0); + instance.setEnd(cal.getTime()); + + // Testing edge cases INSIDE + boolean expResult = true; + boolean result = instance.isDateInTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015, Calendar.AUGUST,10,11,0); + date = cal.getTime(); + result = instance.isDateInTimePeriod(date); + assertEquals(expResult, result); + + // Testing edge cases OUTSIDE + expResult = false; + cal.set(2015,Calendar.AUGUST,5,11,59); + date = cal.getTime(); + result = instance.isDateInTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015, Calendar.AUGUST,10,11,1); + date = cal.getTime(); + result = instance.isDateInTimePeriod(date); + assertEquals(expResult, result); + + // Testing in the middle + cal.set(2015,Calendar.AUGUST,7,12,0); + date = cal.getTime(); + expResult = true; + result = instance.isDateInTimePeriod(date); + assertEquals(expResult, result); + } + + /** + * Test of isDateBeforeTimePeriod method, of class TimePeriod. + */ + public void testIsDateBeforeTimePeriod() { + System.out.println("isDateInTimePeriod"); + Calendar cal = Calendar.getInstance(); + cal.set(2015,Calendar.AUGUST,5,12,0); + cal.set(Calendar.MILLISECOND, 0); + Date date = cal.getTime(); + TimePeriod instance = new TimePeriod(); + instance.setStart(cal.getTime()); + cal.set(2015, Calendar.AUGUST,10,11,0); + instance.setEnd(cal.getTime()); + boolean expResult = false; + boolean result = instance.isDateBeforeTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015,Calendar.AUGUST,5,11,59); + date = cal.getTime(); + expResult = true; + result = instance.isDateBeforeTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015,Calendar.AUGUST,10,12,1); + date = cal.getTime(); + expResult = false; + result = instance.isDateBeforeTimePeriod(date); + assertEquals(expResult, result); + } + + /** + * Test of isDateAfterTimePeriod method, of class TimePeriod. + */ + public void testIsDateAfterTimePeriod() { + System.out.println("isDateInTimePeriod"); + Calendar cal = Calendar.getInstance(); + cal.set(2015,Calendar.AUGUST,5,12,0); + cal.set(Calendar.MILLISECOND, 0); + Date date = cal.getTime(); + TimePeriod instance = new TimePeriod(); + instance.setStart(cal.getTime()); + cal.set(2015, Calendar.AUGUST,10,11,0); + instance.setEnd(cal.getTime()); + boolean expResult = false; + boolean result = instance.isDateAfterTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015,Calendar.AUGUST,5,11,59); + date = cal.getTime(); + expResult = false; + result = instance.isDateAfterTimePeriod(date); + assertEquals(expResult, result); + + cal.set(2015,Calendar.AUGUST,10,12,1); + date = cal.getTime(); + expResult = true; + result = instance.isDateAfterTimePeriod(date); + assertEquals(expResult, result); + + } + + + +}