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 */
021
022package org.biojavax.bio.seq;
023
024
025/**
026 * A simple implementation of the Position interface.
027 * @author Richard Holland
028 * @since 1.5
029 */
030public class SimplePosition implements Position {
031    
032    private boolean fs;
033    private boolean fe;
034    private int s;
035    private int e;
036    private String t;
037    
038    /**
039     * Constructs a point position, with no fuzzy start or
040     * end. (eg. 1, 2, or 3).
041     * @param p the point position
042     */
043    public SimplePosition(int p) {
044        this(false,false,p);
045    }
046    
047    /**
048     * Constructs a range position, with no fuzzy start or
049     * end. (eg. 1..2, 2..5, or 3..8).
050     * @param s the start position
051     * @param e the end position
052     */
053    public SimplePosition(int s, int e) {
054        this(false,false,s,e,null);
055    }
056    
057    /**
058     * Constructs a point position, with optionally fuzzy start and
059     * end. (eg. <1 or 3> or 2 or even <5>).
060     * @param fs fuzzy start?
061     * @param fe fuzzy end?
062     * @param p the point position
063     */
064    public SimplePosition(boolean fs, boolean fe, int p) {
065        this(fs,fe,p,p,null);
066    }
067    
068    /**
069     * Constructs a range position, with optionally fuzzy start and
070     * end. (eg. <1.2 or 1^3> or 2.2 or even <5^6>). The type of the
071     * range is given, it should normally be one of the two defined
072     * in the Position interface, but its up to you.
073     * @param fs fuzzy start?
074     * @param fe fuzzy end?
075     * @param s the start of the position
076     * @param e the end of the position
077     * @param t the type of the position
078     */
079    public SimplePosition(boolean fs, boolean fe, int s, int e, String t) {
080        this.fs = fs;
081        this.fe = fe;
082        this.s = s;
083        this.e = e;
084        this.t = t;
085    }
086    
087    //Hibernate only - futureproofing
088    protected SimplePosition() {}
089    
090    /**
091     * {@inheritDoc}
092     */
093    public boolean getFuzzyStart() { return this.fs; }
094    
095    /**
096     * {@inheritDoc}
097     */
098    public boolean getFuzzyEnd() { return this.fe; }
099    
100    /**
101     * {@inheritDoc}
102     */
103    public int getStart() { return this.s; }
104    
105    /**
106     * {@inheritDoc}
107     */
108    public int getEnd()  { return this.e; }
109    
110    /**
111     * {@inheritDoc}
112     */
113    public String getType() { return this.t; }
114    
115    // Hibernate requirement - not for public use - futureproofing
116    void setFuzzyStart(boolean fs) { this.fs = fs; }
117    
118    // Hibernate requirement - not for public use - futureproofing
119    void setFuzzyEnd(boolean fe) { this.fe = fe; }
120    
121    // Hibernate requirement - not for public use - futureproofing
122    void setStart(int s) { this.s = s; }
123    
124    // Hibernate requirement - not for public use - futureproofing
125    void setEnd(int e) { this.e = e; }
126    
127    // Hibernate requirement - not for public use - futureproofing
128    void setType(String t) { this.t = t; }
129    
130    /** 
131     * {@inheritDoc}
132     */
133    public Position translate(int distance) {
134        return new SimplePosition(this.fs,this.fe,this.s+distance,this.e+distance,this.t);
135    }
136        
137    /** 
138     * {@inheritDoc}
139     * Two positions are equal if they share all parameters in common, 
140     * eg. fuzzy start+end, start, end, type.
141     */
142    public boolean equals(Object o) {
143        if (!(o instanceof Position)) return false;
144        if (o==this) return true;
145        Position them = (Position)o;
146        if (this.getFuzzyStart() != them.getFuzzyStart()) return false;
147        if (this.getFuzzyEnd() != them.getFuzzyEnd()) return false;
148        if (this.getStart()!=them.getStart()) return false;
149        if (this.getEnd()!=them.getEnd()) return false;
150        if (this.getType()!=null || them.getType()!=null) {
151            if (this.getType()!=null && them.getType()!=null) {
152                if (!this.getType().equals(them.getType())) return false;
153            } else return false;
154        }
155        return true;
156    }  
157    
158    /** 
159     * {@inheritDoc}
160     */
161    public String toString() {
162        StringBuffer sb = new StringBuffer();
163        if (this.getFuzzyStart()) sb.append("<");
164        sb.append(this.s);
165        if (s!=e) {
166            sb.append(this.t);
167            sb.append(this.e);
168        }
169        if (this.getFuzzyEnd()) sb.append(">");
170        return sb.toString();
171    }
172    
173    // Hibernate requirement - not for public use - futureproofing
174    private Integer id;
175    
176    /**
177     * Gets the Hibernate ID. Should be used with caution.
178     * @return the Hibernate ID, if using Hibernate.
179     */
180    public Integer getId() { return this.id; }
181    
182    /**
183     * Sets the Hibernate ID. Should be used with caution.
184     * @param id the Hibernate ID, if using Hibernate.
185     */
186    public void setId(Integer id) { this.id = id;}
187    
188}