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