KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > algorithm > Unique


1 // ============================================================================
2
// $Id: Unique.java,v 1.6 2006/02/10 04:44:59 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32
package net.sf.jga.fn.algorithm;
33
34 import java.util.Iterator JavaDoc;
35 import net.sf.jga.fn.BinaryFunctor;
36 import net.sf.jga.fn.UnaryFunctor;
37 import net.sf.jga.fn.comparison.EqualTo;
38 import net.sf.jga.util.UniqueIterator;
39
40 /**
41  * Returns an iterator based on the input iterator that will not yield the
42  * same value twice in succession.
43  * <p>
44  * Copyright &copy; 2003-2005 David A. Hall
45  *
46  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
47  * @deprecated
48  */

49
50 public class Unique<T> extends UnaryFunctor<Iterator JavaDoc<? extends T>, UniqueIterator<T>> {
51     
52     static final long serialVersionUID = 603897787127100783L;
53     
54     private BinaryFunctor<T,T,Boolean JavaDoc> _fn;
55
56     /**
57      * Builds an Unique functor that will use EqualTo to compare successive
58      * elements.
59      * @throws IllegalArgumentException if the test is null
60      */

61     public Unique() {
62         this(new EqualTo<T>());
63     }
64
65     /**
66      * Builds an Unique functor that will use the given functor to compare
67      * successive elements. The functor is required to return TRUE when its
68      * arguments are the same.
69      * @throws IllegalArgumentException if the test is null
70      */

71     public Unique(BinaryFunctor<T,T,Boolean JavaDoc> test) {
72         if (test == null)
73             throw new IllegalArgumentException JavaDoc();
74         
75         _fn = test;
76     }
77
78     /**
79      * Returns the functor used to process elements in an iteration.
80      */

81     public BinaryFunctor<T,T,Boolean JavaDoc> getFunction() {
82         return _fn;
83     }
84
85     /**
86      * Apply the functor to each element in the iteration and return an iterator
87      * over the results
88      *
89      * @return an iterator over the results of the transformation
90      */

91     public UniqueIterator<T> fn(Iterator JavaDoc<? extends T> iterator) {
92         return new UniqueIterator<T>(iterator, _fn);
93     }
94     
95     /**
96      * Calls the Visitor's <code>visit(Unique)</code> method, if it
97      * implements the nested Visitor interface.
98      */

99     public void accept(net.sf.jga.fn.Visitor v) {
100         if (v instanceof Unique.Visitor)
101             ((Unique.Visitor)v).visit(this);
102         else
103             v.visit(this);
104     }
105
106     // Object overrides
107

108     public String JavaDoc toString() {
109         return "Unique["+_fn+"]";
110     }
111     
112     // AcyclicVisitor
113

114     /**
115      * Interface for classes that may interpret an <b>Unique</b> functor.
116      */

117     public interface Visitor extends net.sf.jga.fn.Visitor {
118         public void visit(Unique host);
119     }
120 }
121
Popular Tags