001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.image;
020
021/**
022 * @author Stephane
023 */
024public class ImagePosition implements Comparable<ImagePosition>
025{
026    public static final char T_ID = 'T';
027    public static final char Z_ID = 'Z';
028
029    protected int t;
030    protected int z;
031
032    /**
033     * @param t
034     * @param z
035     */
036    public ImagePosition(int t, int z)
037    {
038        super();
039
040        this.t = t;
041        this.z = z;
042    }
043
044    public ImagePosition()
045    {
046        this(-1, -1);
047    }
048
049    public void copyFrom(ImagePosition ip)
050    {
051        t = ip.t;
052        z = ip.z;
053    }
054
055    public void switchLeft()
056    {
057        t = z;
058        z = 0;
059    }
060
061    public void switchRight()
062    {
063        z = t;
064        t = 0;
065    }
066
067    /**
068     * @return the t
069     */
070    public int getT()
071    {
072        return t;
073    }
074
075    /**
076     * @param t
077     *        the t to set
078     */
079    public void setT(int t)
080    {
081        this.t = t;
082    }
083
084    /**
085     * @return the z
086     */
087    public int getZ()
088    {
089        return z;
090    }
091
092    /**
093     * @param z
094     *        the z to set
095     */
096    public void setZ(int z)
097    {
098        this.z = z;
099    }
100
101    public void set(int t, int z)
102    {
103        this.t = t;
104        this.z = z;
105    }
106
107    public int get(char ident)
108    {
109        final char id = Character.toUpperCase(ident);
110
111        switch (id)
112        {
113            case T_ID:
114                return t;
115
116            case Z_ID:
117                return z;
118        }
119
120        return -1;
121    }
122
123    public static boolean isValidIdentStatic(char ident)
124    {
125        final char id = Character.toUpperCase(ident);
126
127        return (id == T_ID) || (id == Z_ID);
128    }
129
130    public boolean isValidIdent(char ident)
131    {
132        return isValidIdentStatic(ident);
133    }
134
135    public boolean isTUndefined()
136    {
137        return (t == -1);
138    }
139
140    public boolean isZUndefined()
141    {
142        return (z == -1);
143    }
144
145    public boolean isUndefined()
146    {
147        return isTUndefined() || isZUndefined();
148    }
149
150    public char getFirstEmptyPos()
151    {
152        if (isTUndefined())
153            return T_ID;
154        if (isZUndefined())
155            return Z_ID;
156
157        // no empty pos
158        return ' ';
159    }
160
161    public char getLastEmptyPos()
162    {
163        if (isZUndefined())
164            return Z_ID;
165        if (isTUndefined())
166            return T_ID;
167
168        // no empty pos
169        return ' ';
170    }
171
172    public boolean isSamePos(ImagePosition ip, char posIdent)
173    {
174        final char id = Character.toUpperCase(posIdent);
175
176        switch (id)
177        {
178            case T_ID:
179                if (t == -1)
180                    return false;
181                return (ip.t == t);
182
183            case Z_ID:
184                if ((t == -1) || (z == -1))
185                    return false;
186                return (ip.t == t) && (ip.z == z);
187        }
188
189        return false;
190    }
191
192    @Override
193    public int compareTo(ImagePosition o)
194    {
195        final ImagePosition ip = o;
196
197        final int ot = ip.t;
198        final int oz = ip.z;
199
200        if (t > ot)
201            return 1;
202        if (t < ot)
203            return -1;
204        if (z > oz)
205            return 1;
206        if (z < oz)
207            return -1;
208
209        return 0;
210    }
211}