• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.jme3.network;
34 
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.HashSet;
38 
39 /**
40  *  Static utility methods pertaining to Filter instances.
41  *
42  *  @version   $Revision: 8843 $
43  *  @author    Paul Speed
44  */
45 public class Filters
46 {
47     /**
48      *  Creates a filter that returns true for any value in the specified
49      *  list of values and false for all other cases.
50      */
in( T... values )51     public static <T> Filter<T> in( T... values )
52     {
53         return in( new HashSet<T>(Arrays.asList(values)) );
54     }
55 
56     /**
57      *  Creates a filter that returns true for any value in the specified
58      *  collection and false for all other cases.
59      */
in( Collection<? extends T> collection )60     public static <T> Filter<T> in( Collection<? extends T> collection )
61     {
62         return new InFilter<T>(collection);
63     }
64 
65     /**
66      *  Creates a filter that returns true for any value NOT in the specified
67      *  list of values and false for all other cases.  This is the equivalent
68      *  of calling not(in(values)).
69      */
notIn( T... values )70     public static <T> Filter<T> notIn( T... values )
71     {
72         return not( in( values ) );
73     }
74 
75     /**
76      *  Creates a filter that returns true for any value NOT in the specified
77      *  collection and false for all other cases.  This is the equivalent
78      *  of calling not(in(collection)).
79      */
notIn( Collection<? extends T> collection )80     public static <T> Filter<T> notIn( Collection<? extends T> collection )
81     {
82         return not( in( collection ) );
83     }
84 
85     /**
86      *  Creates a filter that returns true for inputs that are .equals()
87      *  equivalent to the specified value.
88      */
equalTo( T value )89     public static <T> Filter<T> equalTo( T value )
90     {
91         return new EqualToFilter<T>(value);
92     }
93 
94     /**
95      *  Creates a filter that returns true for inputs that are NOT .equals()
96      *  equivalent to the specified value.  This is the equivalent of calling
97      *  not(equalTo(value)).
98      */
notEqualTo( T value )99     public static <T> Filter<T> notEqualTo( T value )
100     {
101         return not(equalTo(value));
102     }
103 
104     /**
105      *  Creates a filter that returns true when the specified delegate filter
106      *  returns false, and vice versa.
107      */
not( Filter<T> f )108     public static <T> Filter<T> not( Filter<T> f )
109     {
110         return new NotFilter<T>(f);
111     }
112 
113     private static class EqualToFilter<T> implements Filter<T>
114     {
115         private T value;
116 
EqualToFilter( T value )117         public EqualToFilter( T value )
118         {
119             this.value = value;
120         }
121 
apply( T input )122         public boolean apply( T input )
123         {
124             return value == input || (value != null && value.equals(input));
125         }
126     }
127 
128     private static class InFilter<T> implements Filter<T>
129     {
130         private Collection<? extends T> collection;
131 
InFilter( Collection<? extends T> collection )132         public InFilter( Collection<? extends T> collection )
133         {
134             this.collection = collection;
135         }
136 
apply( T input )137         public boolean apply( T input )
138         {
139             return collection.contains(input);
140         }
141     }
142 
143     private static class NotFilter<T> implements Filter<T>
144     {
145         private Filter<T> delegate;
146 
NotFilter( Filter<T> delegate )147         public NotFilter( Filter<T> delegate )
148         {
149             this.delegate = delegate;
150         }
151 
apply( T input )152         public boolean apply( T input )
153         {
154             return !delegate.apply(input);
155         }
156     }
157 }
158 
159 
160