001/**
002 * 
003 */
004package icy.image;
005
006import icy.sequence.Sequence;
007
008/**
009 * Define a channel position for {@link Sequence} class.
010 * 
011 * @author Stephane
012 */
013public class ChannelPosition extends ImagePosition
014{
015    public static final char C_ID_0 = 'C';
016    public static final char C_ID_1 = 'W';
017
018    protected int c;
019
020    public ChannelPosition(int t, int z, int c)
021    {
022        super(t, z);
023
024        this.c = c;
025    }
026
027    public ChannelPosition(ChannelPosition cp)
028    {
029        this(cp.t, cp.z, cp.c);
030    }
031
032    public ChannelPosition()
033    {
034        this(-1, -1, -1);
035    }
036
037    public void copyFrom(ChannelPosition cp)
038    {
039        t = cp.t;
040        z = cp.z;
041        c = cp.c;
042    }
043
044    @Override
045    public void switchLeft()
046    {
047        t = z;
048        z = c;
049        c = 0;
050    }
051
052    @Override
053    public void switchRight()
054    {
055        c = z;
056        z = t;
057        t = 0;
058    }
059
060    /**
061     * @return the c
062     */
063    public int getC()
064    {
065        return c;
066    }
067
068    /**
069     * @param c
070     *        the c to set
071     */
072    public void setC(int c)
073    {
074        this.c = c;
075    }
076
077    public void set(int t, int z, int c)
078    {
079        super.set(t, z);
080        this.c = c;
081    }
082
083    @Override
084    public int get(char ident)
085    {
086        final char id = Character.toUpperCase(ident);
087
088        switch (id)
089        {
090            case C_ID_0:
091            case C_ID_1:
092                return c;
093        }
094
095        return super.get(ident);
096    }
097
098    public static boolean isValidIdentStatic(char ident)
099    {
100        final char id = Character.toUpperCase(ident);
101
102        return ImagePosition.isValidIdentStatic(ident) || (id == C_ID_0) || (id == C_ID_1);
103    }
104
105    @Override
106    public boolean isValidIdent(char ident)
107    {
108        return isValidIdentStatic(ident);
109    }
110
111    public boolean isCUndefined()
112    {
113        return (c == -1);
114    }
115
116    @Override
117    public boolean isUndefined()
118    {
119        return isCUndefined() || super.isUndefined();
120    }
121
122    /**
123     * Return first undefined position with following priority C -> T -> Z
124     */
125    public char getAlternateFirstEmptyPos()
126    {
127        // check in own position
128        if (isCUndefined())
129            return C_ID_0;
130
131        return super.getFirstEmptyPos();
132    }
133
134    /**
135     * Return first undefined position with following priority T -> Z -> C
136     */
137    @Override
138    public char getFirstEmptyPos()
139    {
140        final char result = super.getFirstEmptyPos();
141
142        // parent doesn't have any spare position
143        if (result == ' ')
144        {
145            // check in own position
146            if (isCUndefined())
147                return C_ID_0;
148        }
149
150        return result;
151    }
152
153    /**
154     * Return last undefined position with following priority Z -> T -> C
155     */
156    public char getAlternateLastEmptyPos()
157    {
158        final char result = super.getLastEmptyPos();
159
160        // parent doesn't have any spare position
161        if (result == ' ')
162        {
163            // check in own position
164            if (isCUndefined())
165                return C_ID_0;
166        }
167
168        return result;
169    }
170
171    /**
172     * Return last undefined position with following priority C -> Z -> T
173     */
174    @Override
175    public char getLastEmptyPos()
176    {
177        // check in own position
178        if (isCUndefined())
179            return C_ID_0;
180
181        return super.getLastEmptyPos();
182    }
183
184    public boolean isSamePos(ChannelPosition cp, char posIdent)
185    {
186        final char id = Character.toUpperCase(posIdent);
187
188        switch (id)
189        {
190            case C_ID_0:
191            case C_ID_1:
192                if ((t == -1) || (z == -1) || (c == -1))
193                    return false;
194                return (cp.t == t) && (cp.z == z) && (cp.c == c);
195
196        }
197
198        return super.isSamePos(cp, posIdent);
199    }
200
201    /**
202     * Compare to another ImagePosition with following priority T -> Z -> C
203     */
204    @Override
205    public int compareTo(ImagePosition o)
206    {
207        final int result = super.compareTo(o);
208
209        if ((result == 0) && (o instanceof ChannelPosition))
210        {
211            final int cp = ((ChannelPosition) o).c;
212
213            if (c > cp)
214                return 1;
215            if (c < cp)
216                return -1;
217        }
218
219        return result;
220    }
221
222    /**
223     * Compare to another BandPosition with following priority C -> T -> Z
224     */
225    public int alternateCompareTo(ChannelPosition cp)
226    {
227        final int oc = cp.c;
228
229        if (c > oc)
230            return 1;
231        if (c < oc)
232            return -1;
233
234        return super.compareTo(cp);
235    }
236}