Mercurial > hg > cmdp
diff src/uk/ac/qmul/eecs/depic/patterns/Range.java @ 0:3074a84ef81e
first import
author | Fiore Martin <f.martin@qmul.ac.uk> |
---|---|
date | Wed, 26 Aug 2015 16:16:53 +0100 |
parents | |
children | 629262395647 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/uk/ac/qmul/eecs/depic/patterns/Range.java Wed Aug 26 16:16:53 2015 +0100 @@ -0,0 +1,87 @@ +/* + Cross-Modal DAW Prototype - Prototype of a simple Cross-Modal Digital Audio Workstation. + + Copyright (C) 2015 Queen Mary University of London (http://depic.eecs.qmul.ac.uk/) + + 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 uk.ac.qmul.eecs.depic.patterns; + +/** + * + * Immutable class containing two comparable number which represent the start and the end of a range. + * + * @param <T> + */ + public class Range<T extends Number & Comparable<T> > { + protected T start; + protected T end; + + public Range(T t1, T t2){ + if(t1.compareTo(t2) < 1){ + start = t1; + end = t2; + }else{ + start = t2; + end = t1; + } + } + + /** + * Creates a new instance by comparing {@code mm1} and {@code mm2}. + * + * The minimum of this object will be the minimum value between {@code mm1.getMin()} and {@code mm2.getMin()}. + * The maximum of this object will be the maximum value between {@code mm1.getMax()} and {@code mm2.getMax()}. + * + * @param r1 the former Range whose min and max are to be compared + * @param r2 the latter Range whose min and max are to be compared + */ + public Range(Range<T> r1, Range<T> r2){ + if(r1.getStart().compareTo(r2.getStart()) < 0){ // if the minimum of mm1 is less than the minimum of mm2 + start = r1.getStart(); // then min is the minimum of mm1 + }else{ // else + start = r2.getStart(); // min is the minimum of mm2 + } + + if(r1.getEnd().compareTo(r2.getEnd()) > 0){ // if the maximum of mm1 is greater than the maximum of mm2 + end = r1.getEnd(); // then max is the maximum of mm1 + }else{ // else + end = r2.getEnd(); // max is the maximum of mm2 + } + } + + protected Range(){ + // empty range + } + + public T getStart() { + return start; + } + public T getEnd() { + return end; + } + + public float lenght(){ + return getEnd().floatValue() - getStart().floatValue(); + } + + @Override + public String toString(){ + return "Range ["+ getStart()+","+getEnd()+"]"; + } + + public static final Range<Float> NORMALIZED_RANGE_F = new Range<>(0.0f,1.0f); + public static final Range<Double> NORMALIZED_RANGE_D = new Range<>(0.0,1.0); + } +