Skip to content
Snippets Groups Projects
Commit 258b637a authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

New functionality

parent f183a8f0
Branches
Tags
No related merge requests found
/*
* 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;
}
}
/*
* 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());
}
}
/*
* 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment