001 /*
002 * BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence. This should
006 * be distributed with the code. If you do not have a copy,
007 * see:
008 *
009 * http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors. These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 * http://www.biojava.org/
019 *
020 * Created on 01-21-2010
021 */
022 package org.biojava3.core.sequence.location;
023
024 import org.biojava3.core.sequence.location.template.Point;
025 import org.biojava3.core.util.Hashcoder;
026 import org.biojava3.core.util.Equals;
027
028 /**
029 * Basic implementation of the Point interface.
030 *
031 * @author ayates
032 */
033 public class SimplePoint implements Point {
034
035 private int position;
036 private boolean unknown;
037 private boolean uncertain;
038
039 protected SimplePoint() {
040 super();
041 }
042
043 public SimplePoint(int position) {
044 this.position = position;
045 }
046
047 public SimplePoint(int position, boolean unknown, boolean uncertain) {
048 this.position = position;
049 this.unknown = unknown;
050 this.uncertain = uncertain;
051 }
052
053 @Override
054 public Integer getPosition() {
055 return position;
056 }
057
058 protected void setPosition(int position) {
059 this.position = position;
060 }
061
062 @Override
063 public boolean isUnknown() {
064 return unknown;
065 }
066
067 protected void setUnknown(boolean unknown) {
068 this.unknown = unknown;
069 }
070
071 @Override
072 public boolean isUncertain() {
073 return uncertain;
074 }
075
076 protected void setUncertain(boolean uncertain) {
077 this.uncertain = uncertain;
078 }
079
080 @Override
081 public Point reverse(int length) {
082 int translatedPosition = reverse(getPosition(), length);
083 return new SimplePoint(translatedPosition, isUnknown(), isUncertain());
084 }
085
086 @Override
087 public Point offset(int distance) {
088 int offsetPosition = getPosition() + distance;
089 return new SimplePoint(offsetPosition, isUnknown(), isUncertain());
090 }
091
092 protected int reverse(int position, int length) {
093 return (length - position) + 1;
094 }
095
096 @Override
097 @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
098 public boolean equals(Object obj) {
099 boolean equals = false;
100 if (Equals.classEqual(this, obj)) {
101 SimplePoint p = (SimplePoint) obj;
102 equals = (Equals.equal(getPosition(), p.getPosition())
103 && Equals.equal(isUncertain(), p.isUncertain())
104 && Equals.equal(isUnknown(), p.isUnknown()));
105 }
106 return equals;
107 }
108
109 @Override
110 public int hashCode() {
111 int r = Hashcoder.SEED;
112 r = Hashcoder.hash(r, getPosition());
113 r = Hashcoder.hash(r, isUncertain());
114 r = Hashcoder.hash(r, isUnknown());
115 return r;
116 }
117
118 @Override
119 public String toString() {
120 return Integer.toString(getPosition());
121 }
122
123 @Override
124 public int compareTo(Point o) {
125 return getPosition().compareTo(o.getPosition());
126 }
127
128 @Override
129 public boolean isLower(Point point) {
130 return (compareTo(point) < 0);
131 }
132
133 @Override
134 public boolean isHigher(Point point) {
135 return (compareTo(point) > 0);
136 }
137
138 @Override
139 public Point clonePoint() {
140 return this.offset(0);
141 }
142 }