X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fslice%2FValueRange.java;fp=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fslice%2FValueRange.java;h=5e5d1e71f8ef1bc62ba1860e82ff39a71c7c8be0;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/slice/ValueRange.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/slice/ValueRange.java new file mode 100644 index 000000000..5e5d1e71f --- /dev/null +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/slice/ValueRange.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.datastructures.slice; + +/** + * @author Tuukka Lehtonen + */ +public class ValueRange { + + public static final ValueRange NO_RANGE = new ValueRange(0, Integer.MAX_VALUE); + public static final ValueRange EMPTY = new ValueRange(0, 0); + public static final ValueRange ONE = new ValueRange(0, 1); + public static final ValueRange TWO = new ValueRange(0, 2); + + private final int start; + private final int size; + + public static ValueRange make(int size) { + if (size == 0) + return EMPTY; + else if (size == 1) + return ONE; + else if (size == 2) + return TWO; + return new ValueRange(size); + } + + public static ValueRange make(int start, int size) { + if (start == 0) { + if (size == 0) + return EMPTY; + else if (size == 1) + return ONE; + else if (size == 2) + return TWO; + } + return new ValueRange(start, size); + } + + private ValueRange(int size) { + this(0, size); + } + + private ValueRange(int start, int size) { + if (start < 0) + throw new IllegalArgumentException("negative range start: " + start); + if (size < 0) + throw new IllegalArgumentException("negative range size: " + size); + this.start = start; + this.size = size; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean isSingle() { + return size == 1; + } + + public int start() { + return start; + } + + public int end() { + return start + size; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if (isEmpty()) + return "[]"; + if (isSingle()) + return "[" + start + "]"; + return "[" + start + "-" + (start + size - 1) + "]"; + } + + public String toString(int offset) { + if (isEmpty()) + return "[]"; + if (isSingle()) + return "[" + (start+offset) + "]"; + return "[" + (start+offset) + "-" + (start + size - 1 + offset) + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + size; + result = prime * result + start; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ValueRange other = (ValueRange) obj; + return size == other.size && start == other.start; + } + +// public ChildReference toChildReference() { +// if (isEmpty()) +// throw new UnsupportedOperationException("cannot create a value reference for an empty range"); +// if (isSingle()) +// return new IndexReference(start); +// //throw new RangeReference(start, start+size-1); +// throw new UnsupportedOperationException("cannot create a value reference for a range with cardinality > 1"); +// } + + /** + * @return a Simantics Databoard child reference string describing this range. + */ + public String toChildReferenceString() { + if (isEmpty()) + throw new UnsupportedOperationException("cannot create a value reference for an empty range"); + StringBuilder sb = new StringBuilder(); + if (isSingle()) + return sb.append("i-").append(start).toString(); + // TODO: proposal: http://dev.simantics.org/index.php/Talk:Databoard_Specification + return sb.append("r-").append(start).append('-').append(start+size-1).toString(); + } + +}