/** * Creates a {@link UniqueList} that determines uniqueness using the * specified {@link Comparator}. * * @param source the {@link EventList} containing duplicates to remove * @param comparator the {@link Comparator} used to determine equality */ public UniqueList(EventList<E> source, Comparator<? super E> comparator) { this(new SortedList<E>(source, comparator), (Void) null); }
SortedList<Row> sorted = new SortedList<Row>( list, new Comparator<Row>() { assertEquals( 3, sorted.size() ); assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() ); assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() ); assertEquals( 2, sorted.size() ); assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() ); assertEquals( 3, sorted.size() ); assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() ); assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() ); assertEquals( 2, sorted.size() ); assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() ); assertEquals( 3, sorted.size() ); assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() ); assertEquals( 4, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() ); assertEquals( 2, sorted.size() ); assertEquals( 3, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() ); assertEquals( 4, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
/** * Modify the behaviour of this {@link SortedList} to one of the predefined modes. * * @param mode either {@link #STRICT_SORT_ORDER} or {@link #AVOID_MOVING_ELEMENTS}. */ public void setMode(int mode) { if(mode != STRICT_SORT_ORDER && mode != AVOID_MOVING_ELEMENTS) throw new IllegalArgumentException("Mode must be either SortedList.STRICT_SORT_ORDER or SortedList.AVOID_MOVING_ELEMENTS"); if(mode == this.mode) return; // apply the new mode this.mode = mode; // we need to re-sort the table on the off-chance that an element // was out of order before if(this.mode == STRICT_SORT_ORDER) { setComparator(getComparator()); } } /**
/** * Tests if the specified values should be grouped together. * * @param sourceIndex0 the first index of the source list to test * @param sourceIndex1 the second index of the source list to test * * @return true iff the values at the specified index are equal according * to the Comparator which defines the grouping logic; false if * either index is out of range or the values shall not be grouped */ private boolean groupTogether(int sourceIndex0, int sourceIndex1) { if(sourceIndex0 < 0 || sourceIndex0 >= sortedList.size()) return false; if(sourceIndex1 < 0 || sourceIndex1 >= sortedList.size()) return false; return comparator.compare(sortedList.get(sourceIndex0), sortedList.get(sourceIndex1)) == 0; }
/** {@inheritDoc} */ @Override public int getEndIndex() { // search for the upperThreshold value int index = sortedSource.lastSortIndex(new Integer(upperThreshold)); // if the upperThreshold exists in the sortedSource, convert the exclusive index to an inclusive index if (index < sortedSource.size() && evaluator.evaluate(sortedSource.get(index)) == upperThreshold) index++; return index; }
public void addHours(final ProjectActivity activity) { final HoursByProject newHoursByProject = new HoursByProject(activity.getProject(), activity.getDuration()); if (this.hoursByProjectList.contains(newHoursByProject)) { HoursByProject HoursByProject = this.hoursByProjectList.get(hoursByProjectList.indexOf(newHoursByProject)); HoursByProject.addHours(newHoursByProject.getHours()); } else { this.hoursByProjectList.add(newHoursByProject); } }
final Comparator<? super E> sourceComparator = sortedSource.getComparator(); if (sourceComparator != grouper.getComparator()) { if (!listChanges.isReordering()) { b.nextBlack(); int sourceIndex = b.getIndex(); updates.elementDeleted(0, sortedSource.get(reverseReorderingMap[sourceIndex])); b.nextBlack(); int sourceIndex = b.getIndex(); updates.elementInserted(uniqueIndex, sortedSource.get(sourceIndex)); uniqueIndex++;
/** * Creates a {@link SortedList} that sorts the specified {@link EventList} * using the specified {@link Comparator} to determine sort order. If the * specified {@link Comparator} is <code>null</code>, then this {@link List} * will be unsorted. */ public SortedList(EventList<E> source, Comparator<? super E> comparator) { super(source); setComparator(comparator); source.addListEventListener(this); }
/** * Returns the index in this list of the first occurrence of the specified * <code>element</code>, or -1 if this list does not contain this * <code>element</code>. More formally, returns the lowest index <tt>i</tt> * such that <tt>uniqueListComparator.compare(get(i), element) == 0</tt>, * or -1 if there is no such index. * * <p>Note: This is a departure from the contract for {@link List#indexOf} * since it does not guarantee that <tt>element.equals(get(i))</tt> where i * is a positive index returned from this method. * * @param element the element to search for. * @return the index in this list of the first occurrence of the specified * element, or -1 if this list does not contain this element * @throws ClassCastException if the type of the specified element * is incompatible with this list */ @Override public int indexOf(Object element) { final int index = Collections.binarySearch(this, (E) element, ((SortedList<E>)source).getComparator()); // if the element is not found (index is negative) then return -1 to indicate the list does not contain it return index < 0 ? -1 : index; }
public void dispose() { if(sortedList != null) { sortedList.dispose(); } sourceNodes.dispose(); } }
private SequenceList(SortedList<E> source, Sequencer<E> sequencer, Comparator<? super E> comparator) { super(source); if (sequencer == null) throw new IllegalArgumentException("sequencer may not be null"); if (comparator == null) throw new IllegalArgumentException("comparator may not be null"); this.sequencer = sequencer; this.comparator = comparator; this.updateSequence(); source.addListEventListener(this); }
/** * Updates the comparator in use and applies it to the table. */ protected void rebuildComparator() { final Comparator<E> rebuiltComparator = sortingState.buildComparator(); // select the new comparator sortedList.getReadWriteLock().writeLock().lock(); try { sortedListComparator = rebuiltComparator; sortedList.setComparator(rebuiltComparator); } finally { sortedList.getReadWriteLock().writeLock().unlock(); } }
/** * Returns the index in this list of the first occurrence of the specified * element, or the index where that element would be in the list if it were * inserted. * * @return the index in this list of the first occurrence of the specified * element, or the index where that element would be in the list if it * were inserted. This will return a value in <tt>[0, size()]</tt>, * inclusive. * * @deprecated Deprecated as of 12/11/2005. Replaced with {@link #sortIndex(Object)} * which has cleaner semantics. */ public int indexOfSimulated(Object object) { return comparator != null ? ((SimpleTree)sorted).indexOfValue(object, true, true, ALL_COLORS) : size(); }
public void actionPerformed(final ActionEvent event) { // 1. Get selected activities final int[] selectionIndices = table.getSelectedRows(); // 2. Remove all selected activities final List<ProjectActivity> selectedActivities = new ArrayList<>(selectionIndices.length); for (int selectionIndex : selectionIndices) { selectedActivities.add( model.getActivitiesList().get(selectionIndex) ); } model.removeActivities(selectedActivities, this); }
/** {@inheritDoc} */ @Override public boolean contains(Object object) { return indexOf(object) != -1; }
public void addHours(final ProjectActivity activity) { final HoursByDay newHoursByDay = new HoursByDay(activity.getStart(), activity.getDuration()); if (this.hoursByDayList.contains(newHoursByDay)) { HoursByDay HoursByDay = this.hoursByDayList.get(hoursByDayList.indexOf(newHoursByDay)); HoursByDay.addHours(newHoursByDay.getHours()); } else { this.hoursByDayList.add(newHoursByDay); } }
/** * Tests if the specified values should be grouped together. * * @param sourceIndex0 the first index of the source list to test * @param sourceIndex1 the second index of the source list to test * * @return true iff the values at the specified index are equal according * to the Comparator which defines the grouping logic; false if * either index is out of range or the values shall not be grouped */ private boolean groupTogether(int sourceIndex0, int sourceIndex1) { if(sourceIndex0 < 0 || sourceIndex0 >= sortedList.size()) return false; if(sourceIndex1 < 0 || sourceIndex1 >= sortedList.size()) return false; return comparator.compare(sortedList.get(sourceIndex0), sortedList.get(sourceIndex1)) == 0; }
/** {@inheritDoc} */ @Override public int getEndIndex() { // search for the upperThreshold value int index = sortedSource.lastSortIndex(new Integer(upperThreshold)); // if the upperThreshold exists in the sortedSource, convert the exclusive index to an inclusive index if (index < sortedSource.size() && evaluator.evaluate(sortedSource.get(index)) == upperThreshold) index++; return index; }
final Comparator<? super E> sourceComparator = sortedSource.getComparator(); if (sourceComparator != grouper.getComparator()) { if (!listChanges.isReordering()) { b.nextBlack(); int sourceIndex = b.getIndex(); updates.elementDeleted(0, sortedSource.get(reverseReorderingMap[sourceIndex])); b.nextBlack(); int sourceIndex = b.getIndex(); updates.elementInserted(uniqueIndex, sortedSource.get(sourceIndex)); uniqueIndex++;
/** * Change the {@link Comparator} which determines the groupings presented * by this List * * @param comparator the {@link Comparator} used to determine groupings; * <tt>null</tt> will be treated as {@link GlazedLists#comparableComparator()} */ public void setComparator(Comparator<? super E> comparator) { if (comparator == null) comparator = (Comparator) GlazedLists.comparableComparator(); ((SortedList<E>) source).setComparator(comparator); }