|
From: <de...@us...> - 2013-01-03 22:57:12
|
Revision: 8174
http://fudaa.svn.sourceforge.net/fudaa/?rev=8174&view=rev
Author: deniger
Date: 2013-01-03 22:57:03 +0000 (Thu, 03 Jan 2013)
Log Message:
-----------
Modified Paths:
--------------
trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluListSelection.java
Added Paths:
-----------
trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/CtuluListSelectionTest.java
Modified: trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluListSelection.java
===================================================================
--- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluListSelection.java 2013-01-03 14:12:49 UTC (rev 8173)
+++ trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/CtuluListSelection.java 2013-01-03 22:57:03 UTC (rev 8174)
@@ -17,8 +17,8 @@
import javax.swing.ListSelectionModel;
/**
- * Liste de selection encapsulant un BitSet. Des methodes sont rajoutees pour gerer les operations avec d'autres listes d'index ou
- * des inversions de selection dans un intervalle donne.
+ * Liste de selection encapsulant un BitSet. Des methodes sont rajoutees pour gerer les operations avec d'autres listes d'index ou des inversions de
+ * selection dans un intervalle donne.
*
* @see java.util.BitSet utilisation de BitSet
* @version $Id: CtuluListSelection.java,v 1.8.4.1 2008-05-13 12:10:48 bmarchan Exp $
@@ -70,6 +70,8 @@
*/
private Set listeners_;
CtuluListSelectionEvent evt_;
+ private boolean maxComputed = false;
+ private int max = -1;
/**
* initialise la liste de selection avec comme taille 32.
@@ -109,7 +111,7 @@
}
public static BitSet createForSelectedIdx(final int[] _b) {
- if (_b == null|| _b.length==0) {
+ if (_b == null || _b.length == 0) {
return null;
}
BitSet res = new BitSet(_b[_b.length - 1]);
@@ -149,6 +151,7 @@
private boolean add(final int _i, final boolean _event) {
if (!isSelected(_i)) {
index_.set(_i);
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -162,6 +165,7 @@
for (int i = _deb; i <= _fin; i++) {
r |= add(i, false);
}
+ maxComputed = false;
if (r && _event) {
fireSelectionEvent();
}
@@ -173,6 +177,7 @@
return false;
}
removeInterval(0, getMaxIndex(), false);
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -198,6 +203,7 @@
private void inverse(final int _taille, final boolean _event) {
if (isEmpty()) {
setSelectionInterval(0, _taille - 1, false);
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -218,6 +224,7 @@
} else if (tailleIdx < max) {
index_.clear(_taille, max + 1);
}
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -234,6 +241,7 @@
return false;
}
index_.or(_l.index_);
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -243,6 +251,7 @@
private boolean remove(final int _i, final boolean _event) {
if (isSelected(_i)) {
index_.clear(_i);
+ maxComputed = false;
if (_event) {
fireSelectionEvent();
}
@@ -262,6 +271,7 @@
for (int i = _deb; i <= _fin; i++) {
r |= remove(i, false);
}
+ maxComputed = false;
if (r && _event) {
fireSelectionEvent();
}
@@ -272,6 +282,7 @@
boolean r = removeInterval(0, _deb - 1, false);
r |= addInterval(_deb, _fin, false);
r |= removeInterval(_fin + 1, getMaxIndex(), false);
+ maxComputed = false;
if ((listeners_ != null) && r && _event) {
fireSelectionEvent();
}
@@ -330,6 +341,7 @@
return false;
} else if (!_l.contains(this)) {
index_.and(_l.index_);
+ maxComputed = false;
if (listeners_ != null) {
fireSelectionEvent();
}
@@ -441,7 +453,12 @@
*/
@Override
public int getMaxIndex() {
- return index_.length() - 1;
+ if (maxComputed) {
+ return max;
+ }
+ max = index_.length() - 1;
+ maxComputed = true;
+ return max;
}
@Override
@@ -621,6 +638,7 @@
remove(i, false);
}
}
+ maxComputed = false;
if ((listeners_ != null) && r) {
fireSelectionEvent();
}
@@ -653,6 +671,7 @@
}
clear(false);
or(_l, false);
+ maxComputed = false;
if (listeners_ != null) {
fireSelectionEvent();
}
@@ -669,6 +688,7 @@
index_.set(i);
}
}
+ maxComputed = false;
if (listeners_ != null) {
fireSelectionEvent();
}
@@ -685,9 +705,11 @@
s.set(i);
}
}
+
if (!s.equals(index_)) {
clear(false);
index_.or(s);
+ maxComputed = false;
fireSelectionEvent();
}
}
@@ -712,6 +734,7 @@
return false;
}
index_.xor(_l.index_);
+ maxComputed = false;
if (listeners_ != null) {
fireSelectionEvent();
}
Added: trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/CtuluListSelectionTest.java
===================================================================
--- trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/CtuluListSelectionTest.java (rev 0)
+++ trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/CtuluListSelectionTest.java 2013-01-03 22:57:03 UTC (rev 8174)
@@ -0,0 +1,165 @@
+/*
+ GPL 2
+ */
+package org.fudaa.ctulu;
+
+import java.util.BitSet;
+import junit.framework.TestCase;
+
+/**
+ *
+ * @author Frederic Deniger
+ */
+public class CtuluListSelectionTest extends TestCase {
+
+ public CtuluListSelectionTest() {
+ }
+
+ public void testCreate() {
+ CtuluListSelection selection = new CtuluListSelection();
+ assertEquals(-1, selection.getMaxIndex());
+ assertEquals(-1, selection.getMinIndex());
+
+ BitSet set = new BitSet();
+ set.set(2);
+ selection = new CtuluListSelection(set);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+
+ selection = new CtuluListSelection(selection);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+
+
+ selection = new CtuluListSelection(10);
+ assertEquals(-1, selection.getMaxIndex());
+ assertEquals(-1, selection.getMinIndex());
+
+
+ }
+
+ public void testAdd() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(5, selection.getMinIndex());
+ }
+
+ public void testAddList() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(5, selection.getMinIndex());
+
+ CtuluListSelection selectionToAdd = new CtuluListSelection();
+ selectionToAdd.add(2);
+ selectionToAdd.add(6);
+ selection.add(selectionToAdd);
+ assertEquals(6, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testInverse() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ selection.inverse();
+ assertEquals(4, selection.getMaxIndex());
+ assertEquals(0, selection.getMinIndex());
+
+ selection.inverse(10);
+ assertEquals(9, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testRemove() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ selection.remove(5);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+
+ }
+
+ public void testRemoveList() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ CtuluListSelection selectionToRemove = new CtuluListSelection();
+ selectionToRemove.add(5);
+ selection.remove(selectionToRemove);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+
+ }
+
+ public void testAddInterval() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ selection.addInterval(1, 10);
+ assertEquals(10, selection.getMaxIndex());
+ assertEquals(1, selection.getMinIndex());
+ }
+
+ public void testRemoveInterval() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ selection.removeInterval(4, 10);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testAnd() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ CtuluListSelection selectionAnd = new CtuluListSelection();
+ selectionAnd.add(2);
+ selection.and(selectionAnd);
+ assertEquals(2, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testOr() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ CtuluListSelection selectionOr = new CtuluListSelection();
+ selectionOr.add(6);
+ selection.or(selectionOr);
+ assertEquals(6, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testXOr() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ CtuluListSelection selectionOr = new CtuluListSelection();
+ selectionOr.add(5);
+ selectionOr.add(3);
+ selection.xor(selectionOr);
+ assertEquals(3, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ }
+
+ public void testClear() {
+ CtuluListSelection selection = new CtuluListSelection();
+ selection.add(5);
+ selection.add(2);
+ assertEquals(5, selection.getMaxIndex());
+ assertEquals(2, selection.getMinIndex());
+ selection.clear();
+ assertEquals(-1, selection.getMaxIndex());
+ assertEquals(-1, selection.getMinIndex());
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <de...@us...> - 2013-04-03 09:20:23
|
Revision: 8370
http://fudaa.svn.sourceforge.net/fudaa/?rev=8370&view=rev
Author: deniger
Date: 2013-04-03 09:20:14 +0000 (Wed, 03 Apr 2013)
Log Message:
-----------
Ajout d'un iterator sur un tableau d'entier
Added Paths:
-----------
trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/iterator/FixedIntegerIterator.java
trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/iterator/
trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/iterator/FixedIntegerIteratorTest.java
Copied: trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/iterator/FixedIntegerIterator.java (from rev 8335, trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/iterator/NumberIterator.java)
===================================================================
--- trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/iterator/FixedIntegerIterator.java (rev 0)
+++ trunk/framework/ctulu-common/src/main/java/org/fudaa/ctulu/iterator/FixedIntegerIterator.java 2013-04-03 09:20:14 UTC (rev 8370)
@@ -0,0 +1,262 @@
+/*
+ * Geotools 2 - OpenSource mapping toolkit
+ * (C) 2003, Geotools Project Managment Committee (PMC)
+ * (C) 2000, Institut de Recherche pour le D\xE9veloppement
+ * (C) 1999, P\xEAches et Oc\xE9ans Canada
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Contacts:
+ * UNITED KINGDOM: James Macgill
+ * mailto:j.m...@ge...
+ * FRANCE: Surveillance de l'Environnement Assist\xE9e par Satellite
+ * Institut de Recherche pour le D\xE9veloppement / US-Espace
+ * mailto:se...@te...
+ *
+ * CANADA: Observatoire du Saint-Laurent
+ * Institut Maurice-Lamontagne
+ * mailto:os...@os...
+ */
+package org.fudaa.ctulu.iterator;
+
+// Miscellaneous
+import com.memoire.fu.FuEmptyArrays;
+import java.util.Arrays;
+import org.apache.commons.lang.StringUtils;
+import org.fudaa.ctulu.CtuluLibArray;
+
+/**
+ * It\xE9rateur balayant les barres et \xE9tiquettes de graduation d'un axe. Cet it\xE9rateur retourne les positions des graduations \xE0 partir de la valeur
+ * minimale jusqu'\xE0 la valeur maximale.
+ *
+ * @version $Id: NumberIterator.java,v 1.1 2007-01-17 10:45:26 deniger Exp $
+ * @author Martin Desruisseaux
+ */
+public class FixedIntegerIterator implements TickIterator {
+
+ private final int[] values;
+ /**
+ * Pour eviter les loop infinie.
+ */
+ private static final int MAX_ITERATION = 200;
+ int iterationDone_;
+ /**
+ * Valeur limite des graduations. La derni\xE8re graduation n'aura pas n\xE9cessairement cette valeur. Cette valeur est fix\xE9e par {@link #init}.
+ */
+ private int maximum_;
+ /**
+ * Valeur de la premi\xE8re graduation principale. Cette valeur est fix\xE9e par {@link #init}.
+ */
+ private int minimum_;
+ /**
+ * Index de la graduation principale en cours de tra\xE7age. Cette valeur commence \xE0 0 et sera modifi\xE9e \xE0 chaque appel \xE0 {@link #next}.
+ */
+ private int idx;
+
+ /**
+ * Construit un it\xE9rateur par d\xE9faut. La m\xE9thode {@link #init(double, double, float, float)}<u>doit </u> \xEAtre appel\xE9e avant que cet it\xE9rateur ne
+ * soit utilisable.
+ */
+ public FixedIntegerIterator(int[] values) {
+ this.values = values == null ? FuEmptyArrays.INT0 : CtuluLibArray.copy(values);
+ Arrays.sort(values);
+ }
+
+ public FixedIntegerIterator(final FixedIntegerIterator _it) {
+ this.values = _it.values;
+ initFrom(_it);
+ // this.locale = locale;
+ }
+
+ protected final void initFrom(final FixedIntegerIterator _it) {
+ if (_it != null) {
+ maximum_ = _it.maximum_;
+ minimum_ = _it.minimum_;
+ idx = _it.idx;
+ }
+ }
+
+ /**
+ * Initialise l'it\xE9rateur.
+ *
+ * @param _minimum Valeur minimale de la premi\xE8re graduation.
+ * @param _maximum Valeur limite des graduations. La derni\xE8re graduation n'aura pas n\xE9cessairement cette valeur.
+ * @param _visualLength Longueur visuelle de l'axe sur laquelle tracer la graduation. Cette longueur doit \xEAtre exprim\xE9e en pixels ou en points.
+ * @param _visualTickSpacing Espace \xE0 laisser visuellement entre deux marques de graduation. Cet espace doit \xEAtre exprim\xE9 en pixels ou en points
+ * (1/72 de pouce).
+ */
+ protected void init(final double _minimum, final double _maximum, final float _visualLength,
+ final float _visualTickSpacing) {
+ if (_minimum == this.minimum_ && _maximum == this.maximum_) {
+ rewind();
+ return;
+ }
+ AssertValues.ensureFinite("minimum", _minimum);
+ AssertValues.ensureFinite("maximum", _maximum);
+ minimum_ = (int) Math.min(_minimum, _maximum);
+ maximum_ = (int) Math.max(_minimum, _maximum);
+
+ initCounters();
+ }
+
+ @Override
+ public void initExact(double _min, double _max, int _nbIteration, int _nbSousIteration) {
+ initExactCommon(_min, _max, _nbSousIteration);
+ }
+
+ private void initExactCommon(double _min, double _max, int _nbSousIteration) {
+ minimum_ = (int) Math.min(_min, _max);
+ maximum_ = (int) Math.max(_min, _max);
+ initCounters();
+ }
+
+ @Override
+ public void initExactFromDist(double _min, double _max, double _increment, int _nbSousIteration) {
+ initExactCommon(_min, _max, _nbSousIteration);
+
+ }
+
+ @Override
+ public final Object clone() throws CloneNotSupportedException {
+ final FixedIntegerIterator res = (FixedIntegerIterator) super.clone();
+ res.initFrom(this);
+ return res;
+ }
+
+ /**
+ * Retourne l'\xE9tiquette de la graduation courante. On n'appele g\xE9n\xE9ralement cette m\xE9thode que pour les graduations majeures, mais elle peut aussi
+ * \xEAtre appel\xE9e pour les graduations mineures. Cette m\xE9thode retourne
+ * <code>null</code> s'il n'y a pas d'\xE9tiquette pour la graduation courante.
+ */
+ @Override
+ public String currentLabel() {
+ if (idx >= 0 && idx < values.length) {
+ return Integer.toString(values[idx]);
+ }
+ return StringUtils.EMPTY;
+ }
+
+ /**
+ * Returns the position where to draw the current tick. The position is scaled from the graduation's minimum to maximum. This is usually the same
+ * number than {@link #currentValue}. The mean exception is for logarithmic graduation, in which the tick position is not proportional to the tick
+ * value.
+ */
+ @Override
+ public double currentPosition() {
+ if (idx >= 0 && idx < values.length) {
+ return values[idx];
+ }
+ return Double.NaN;
+ }
+
+ /**
+ * Retourne la valeur de la graduation courante. Cette m\xE9thode peut \xEAtre appel\xE9e pour une graduation majeure ou mineure.
+ */
+ @Override
+ public double currentValue() {
+ return currentPosition();
+ }
+
+ @Override
+ public String formatSubValue(final double _v) {
+ return Integer.toString((int) _v);
+ }
+
+ @Override
+ public String formatValue(final double _v) {
+ return Integer.toString((int) _v);
+ }
+
+ @Override
+ public double getIncrement() {
+ return 1;
+ }
+
+ /**
+ * Indique s'il reste des graduations \xE0 retourner. Cette m\xE9thode retourne
+ * <code>true</code> tant que {@link #currentValue}ou {@link #currentLabel}peuvent \xEAtre appel\xE9es.
+ */
+ @Override
+ public boolean hasNext() {
+ return iterationDone_ < MAX_ITERATION && idx >= 0 && idx < values.length && values[idx] <= maximum_;
+ }
+
+ /**
+ * Initialise l'it\xE9rateur.
+ *
+ * @param _minimum Valeur minimale de la premi\xE8re graduation.
+ * @param _maximum Valeur limite des graduations. La derni\xE8re graduation n'aura pas n\xE9cessairement cette valeur.
+ * @param _nbTick le nombre de graduation
+ */
+ @Override
+ public void init(final double _minimum, final double _maximum, final int _nbTick) {
+ init(_minimum, _maximum, _nbTick <= 1 ? DEFAULT_STICK_NUMBER : _nbTick, 1);
+ }
+
+ /**
+ * Indique si la graduation courante est une graduation majeure.
+ *
+ * @return <code>true</code> si la graduation courante est une graduation majeure, ou <code>false</code> si elle est une graduation mineure.
+ */
+ @Override
+ public boolean isMajorTick() {
+ return true;
+ }
+
+ @Override
+ public double getValueFromPosition(final double _v) {
+ return _v;
+ }
+
+ @Override
+ public double getPositionFromValue(final double _v) {
+ return _v;
+ }
+
+ /**
+ * Passe \xE0 la graduation suivante.
+ */
+ @Override
+ public void next() {
+ iterationDone_++;
+ idx++;
+ }
+
+ /**
+ * Passe directement \xE0 la graduation majeure suivante.
+ */
+ @Override
+ public void nextMajor() {
+ next();
+ }
+
+ /**
+ * Replace l'it\xE9rateur sur la premi\xE8re graduation.
+ */
+ @Override
+ public void rewind() {
+ initCounters();
+ }
+
+ public void initCounters() {
+ idx = Arrays.binarySearch(values, minimum_);
+ if (idx < 0) {
+ idx = -idx - 1;
+ }
+
+ iterationDone_ = 0;
+ }
+}
\ No newline at end of file
Added: trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/iterator/FixedIntegerIteratorTest.java
===================================================================
--- trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/iterator/FixedIntegerIteratorTest.java (rev 0)
+++ trunk/framework/ctulu-common/src/test/java/org/fudaa/ctulu/iterator/FixedIntegerIteratorTest.java 2013-04-03 09:20:14 UTC (rev 8370)
@@ -0,0 +1,60 @@
+/*
+ GPL 2
+ */
+package org.fudaa.ctulu.iterator;
+
+import java.util.ArrayList;
+import java.util.List;
+import junit.framework.TestCase;
+
+/**
+ *
+ * @author Frederic Deniger
+ */
+public class FixedIntegerIteratorTest extends TestCase {
+
+ public void testIterate() {
+ int[] values = new int[]{-1, 1, 10, 100};
+ FixedIntegerIterator iterator = new FixedIntegerIterator(values);
+ iterator.init(-100, 1000, 1);
+ assertTrue(iterator.hasNext());
+ assertEquals(-1, (int) iterator.currentValue());
+
+
+ List<String> readValues = getValuesForIterator(iterator);
+ assertSame(readValues, new String[]{"-1", "1", "10", "100"});
+
+ iterator.init(-1, 1000, 1);
+ readValues = getValuesForIterator(iterator);
+ assertSame(readValues, new String[]{"-1", "1", "10", "100"});
+
+ iterator.init(-1, 99, 1);
+ readValues = getValuesForIterator(iterator);
+ assertSame(readValues, new String[]{"-1", "1", "10"});
+
+
+ iterator.init(9, 99, 1);
+ readValues = getValuesForIterator(iterator);
+ assertSame(readValues, new String[]{"10"});
+
+
+ iterator.init(101, 1000, 1);
+ readValues = getValuesForIterator(iterator);
+ assertTrue(readValues.isEmpty());
+ }
+
+ private List<String> getValuesForIterator(FixedIntegerIterator iterator) {
+ List<String> readValues = new ArrayList<String>();
+ for (final TickIterator it = iterator; it.hasNext(); it.next()) {
+ readValues.add(iterator.currentLabel());
+ }
+ return readValues;
+ }
+
+ public void assertSame(List<String> readValues, String[] expectedValues) {
+ assertEquals(readValues.size(), expectedValues.length);
+ for (int i = 0; i < expectedValues.length; i++) {
+ assertEquals(expectedValues[i], readValues.get(i));
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|