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 */
022package org.biojava.nbio.core.sequence.location;
023
024import java.io.Serializable;
025
026import org.biojava.nbio.core.sequence.location.template.Point;
027import org.biojava.nbio.core.util.Equals;
028import org.biojava.nbio.core.util.Hashcoder;
029
030/**
031 * Basic implementation of the Point interface.
032 *
033 * @author ayates
034 */
035public class SimplePoint implements Serializable, Point {
036
037
038        private static final long serialVersionUID = 1L;
039
040        private int position;
041        private boolean unknown;
042        private boolean uncertain;
043
044        protected SimplePoint() {
045                super();
046        }
047
048        public SimplePoint(int position) {
049                this.position = position;
050        }
051
052        public SimplePoint(int position, boolean unknown, boolean uncertain) {
053                this.position = position;
054                this.unknown = unknown;
055                this.uncertain = uncertain;
056        }
057
058        @Override
059        public Integer getPosition() {
060                return position;
061        }
062
063        protected void setPosition(int position) {
064                this.position = position;
065        }
066
067        @Override
068        public boolean isUnknown() {
069                return unknown;
070        }
071
072        protected void setUnknown(boolean unknown) {
073                this.unknown = unknown;
074        }
075
076        @Override
077        public boolean isUncertain() {
078                return uncertain;
079        }
080
081        protected void setUncertain(boolean uncertain) {
082                this.uncertain = uncertain;
083        }
084
085        @Override
086        public Point reverse(int length) {
087                int translatedPosition = reverse(getPosition(), length);
088                return new SimplePoint(translatedPosition, isUnknown(), isUncertain());
089        }
090
091        @Override
092        public Point offset(int distance) {
093                int offsetPosition = getPosition() + distance;
094                return new SimplePoint(offsetPosition, isUnknown(), isUncertain());
095        }
096
097        protected int reverse(int position, int length) {
098                return (length - position) + 1;
099        }
100
101        @Override
102        public boolean equals(Object obj) {
103                boolean equals = false;
104                if (Equals.classEqual(this, obj)) {
105                        SimplePoint p = (SimplePoint) obj;
106                        equals = (Equals.equal(getPosition(), p.getPosition())
107                                        && Equals.equal(isUncertain(), p.isUncertain())
108                                        && Equals.equal(isUnknown(), p.isUnknown()));
109                }
110                return equals;
111        }
112
113        @Override
114        public int hashCode() {
115                int r = Hashcoder.SEED;
116                r = Hashcoder.hash(r, getPosition());
117                r = Hashcoder.hash(r, isUncertain());
118                r = Hashcoder.hash(r, isUnknown());
119                return r;
120        }
121
122        @Override
123        public String toString() {
124                return Integer.toString(getPosition());
125        }
126
127        @Override
128        public int compareTo(Point o) {
129                return getPosition().compareTo(o.getPosition());
130        }
131
132        @Override
133        public boolean isLower(Point point) {
134                return (compareTo(point) < 0);
135        }
136
137        @Override
138        public boolean isHigher(Point point) {
139                return (compareTo(point) > 0);
140        }
141
142        @Override
143        public Point clonePoint() {
144                return this.offset(0);
145        }
146}