001 package com.croftsoft.core.util; 002 003 import java.util.*; 004 005 /********************************************************************* 006 * 007 * A List that, like a Set, contains no duplicate Elements. 008 * 009 * @author 010 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 011 * @version 012 * 1998-11-23 013 *********************************************************************/ 014 015 public class SetList implements Set, List 016 ////////////////////////////////////////////////////////////////////// 017 ////////////////////////////////////////////////////////////////////// 018 { 019 020 protected List list; 021 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 025 public SetList ( List list ) 026 ////////////////////////////////////////////////////////////////////// 027 { 028 this.list = list; 029 } 030 031 ////////////////////////////////////////////////////////////////////// 032 ////////////////////////////////////////////////////////////////////// 033 034 /********************************************************************* 035 * Returns false if the List already contains the object. 036 *********************************************************************/ 037 public synchronized boolean add ( Object o ) 038 ////////////////////////////////////////////////////////////////////// 039 { 040 if ( list.contains ( o ) ) return false; 041 else return list.add ( o ); 042 } 043 044 /********************************************************************* 045 * Skips objects in the Collection that already exist in the List. 046 * Returns true if any of the objects were added. 047 *********************************************************************/ 048 public synchronized boolean addAll ( Collection c ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 boolean result = false; 052 Iterator iterator = c.iterator ( ); 053 while ( iterator.hasNext ( ) ) 054 { 055 if ( this.add ( iterator.next ( ) ) ) result = true; 056 } 057 return result; 058 } 059 060 /********************************************************************* 061 * Skips object in the Collection that already exist in the List. 062 * Returns true if any of the objects were added. 063 *********************************************************************/ 064 public synchronized boolean addAll ( int index, Collection c ) 065 ////////////////////////////////////////////////////////////////////// 066 { 067 boolean result = false; 068 int i = 0; 069 Iterator iterator = c.iterator ( ); 070 while ( iterator.hasNext ( ) ) 071 { 072 Object o = iterator.next ( ); 073 if ( !list.contains ( o ) ) 074 { 075 list.add ( index + i, o ); 076 i++; 077 result = true; 078 } 079 } 080 return result; 081 } 082 083 /********************************************************************* 084 * @throws IllegalArgumentException 085 * If a duplicate object already exists in the List. 086 *********************************************************************/ 087 public synchronized Object set ( int index, Object element ) 088 ////////////////////////////////////////////////////////////////////// 089 { 090 if ( list.contains ( element ) ) 091 { 092 throw new IllegalArgumentException ( "duplicate" ); 093 } 094 else return list.set ( index, element ); 095 } 096 097 /********************************************************************* 098 * @throws IllegalArgumentException 099 * If a duplicate object already exists in the List. 100 *********************************************************************/ 101 public synchronized void add ( int index, Object element ) 102 ////////////////////////////////////////////////////////////////////// 103 { 104 if ( list.contains ( element ) ) 105 { 106 throw new IllegalArgumentException ( "duplicate" ); 107 } 108 else list.add ( index, element ); 109 } 110 111 ////////////////////////////////////////////////////////////////////// 112 ////////////////////////////////////////////////////////////////////// 113 114 public int size() { return list.size ( ); } 115 116 public boolean isEmpty() { return list.isEmpty ( ); } 117 118 public boolean contains(Object o) { return list.contains ( o ); } 119 120 public Iterator iterator() { return list.iterator ( ); } 121 122 public Object[] toArray() { return list.toArray ( ); } 123 124 public Object[] toArray(Object a[]) { return list.toArray ( a ); } 125 126 public boolean remove(Object o) { return list.remove ( o ); } 127 128 public boolean containsAll(Collection c) 129 { return list.containsAll ( c ); } 130 131 public boolean removeAll(Collection c) 132 { return list.removeAll ( c ); } 133 134 public boolean retainAll(Collection c) 135 { return list.retainAll ( c ); } 136 137 public void clear ( ) { list.clear ( ); } 138 139 public boolean equals(Object o) { return list.equals ( o ); } 140 141 public int hashCode ( ) { return list.hashCode ( ); } 142 143 public Object get(int index) { return list.get ( index ); } 144 145 public Object remove ( int index ) { return list.remove ( index ); } 146 147 public int indexOf(Object o) { return list.indexOf ( o ); } 148 149 public int lastIndexOf(Object o) { return list.lastIndexOf ( o ); } 150 151 public ListIterator listIterator() { return list.listIterator ( ); } 152 153 public ListIterator listIterator(int index) 154 { return list.listIterator ( index ); } 155 156 public List subList(int fromIndex, int toIndex) 157 { return list.subList ( fromIndex, toIndex ); } 158 159 ////////////////////////////////////////////////////////////////////// 160 ////////////////////////////////////////////////////////////////////// 161 }