Commit a83441f1 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Added serializable intervals.

parent 6dd91a94
......@@ -4,7 +4,7 @@
<groupId>de.cebitec.common</groupId>
<artifactId>intervals</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
<packaging>bundle</packaging>
<name>Intervals library</name>
......
......@@ -27,90 +27,6 @@ import java.util.List;
*/
public class Intervals {
private static abstract class IntervalImpl<T extends Number> implements Interval<T> {
private final Type type;
private final T start;
private final T end;
public IntervalImpl(Type type, T start, T end) {
this.type = type;
this.start = start;
this.end = end;
}
@Override
public T getStart() {
return start;
}
@Override
public T getEnd() {
return end;
}
@Override
public Type getType() {
return type;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + (this.start != null ? this.start.hashCode() : 0);
hash = 89 * hash + (this.end != null ? this.end.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final IntervalImpl<T> other = (IntervalImpl<T>) obj;
if (this.start != other.start && (this.start == null || !this.start.equals(other.start))) {
return false;
}
if (this.end != other.end && (this.end == null || !this.end.equals(other.end))) {
return false;
}
return true;
}
@Override
public String toString() {
return "IntervalImpl{" + "type=" + type + ", start=" + start + ", end=" + end + '}';
}
private static class Integer extends IntervalImpl<java.lang.Integer> implements de.cebitec.common.sequencetools.intervals.Interval.Integer {
public Integer(Type type, java.lang.Integer start, java.lang.Integer end) {
super(type, start, end);
}
@Override
public java.lang.Integer getLength() {
if (Type.ZeroOpen.equals(getType())) {
return getEnd() - getStart();
} else {
return Intervals.normalize(this).getLength();
}
}
@Override
public boolean isEmpty() {
return getLength() == 0;
}
@Override
public Interval<java.lang.Integer> as(Type newType) {
return Intervals.transfromInterval(this, newType);
}
}
}
public static final Interval<Integer> EMPTY = emptyInterval();
public static Interval<Integer> emptyInterval() {
......@@ -145,7 +61,7 @@ public class Intervals {
// if (start > end) {
// throw new IllegalArgumentException("Start must be lower than end.");
// }
return new IntervalImpl.Integer(Type.ZeroOpen, normalizeStart(start, type), normalizeEnd(end, type));
return new SerializableInterval(normalizeStart(start, type), normalizeEnd(end, type), Type.ZeroOpen);
}
/**
......@@ -158,9 +74,10 @@ public class Intervals {
public static Interval<Integer> transfromInterval(Interval<Integer> interval, Type targetType) {
Interval<Integer> normalized = normalize(interval);
return new IntervalImpl.Integer(targetType,
return new SerializableInterval(
deNormalizeStart(normalized.getStart(), targetType),
deNormalizeEnd(normalized.getEnd(), targetType));
deNormalizeEnd(normalized.getEnd(), targetType),
targetType);
}
/**
......
/*
* Copyright (C) 2013 Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.sequencetools.intervals;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*/
@XmlRootElement(name = "interval")
@XmlAccessorType(XmlAccessType.FIELD)
class SerializableInterval implements Serializable, Interval<Integer> {
private java.lang.Integer start;
private java.lang.Integer end;
private Type type;
public SerializableInterval() {
}
public SerializableInterval(java.lang.Integer start, java.lang.Integer end, Type type) {
this.start = start;
this.end = end;
this.type = type;
}
@Override
public java.lang.Integer getStart() {
return start;
}
@Override
public java.lang.Integer getEnd() {
return end;
}
@Override
public Type getType() {
return type;
}
@Override
public java.lang.Integer getLength() {
if (Type.ZeroOpen.equals(getType())) {
return getEnd() - getStart();
} else {
return Intervals.normalize(this).getLength();
}
}
@Override
public boolean isEmpty() {
return getLength() == 0;
}
@Override
public Interval<java.lang.Integer> as(Type newType) {
return Intervals.transfromInterval(this, newType);
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + (this.start != null ? this.start.hashCode() : 0);
hash = 89 * hash + (this.end != null ? this.end.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final SerializableInterval other = (SerializableInterval) obj;
if (this.start != other.start && (this.start == null || !this.start.equals(other.start))) {
return false;
}
if (this.end != other.end && (this.end == null || !this.end.equals(other.end))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Interval{" + "type=" + type + ", start=" + start + ", end=" + end + '}';
}
}
/*
* Copyright (C) 2013 Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.sequencetools.intervals;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.junit.Test;
/**
*
* @author Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*/
public class SerializationTest {
@Test
public void testSerializeInterval() throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(SerializableInterval.class);
Marshaller marshaller = jc.createMarshaller();
Unmarshaller unmarshaller = jc.createUnmarshaller();
Interval<Integer> test = Intervals.createInterval(0, 10);
System.out.println("test = " + test);
StringWriter sw = new StringWriter();
marshaller.marshal(test, sw);
System.out.println("sw = " + sw);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment