1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections;
  17. import java.util.Collection;
  18. import org.apache.commons.collections.functors.AllPredicate;
  19. import org.apache.commons.collections.functors.AndPredicate;
  20. import org.apache.commons.collections.functors.AnyPredicate;
  21. import org.apache.commons.collections.functors.EqualPredicate;
  22. import org.apache.commons.collections.functors.ExceptionPredicate;
  23. import org.apache.commons.collections.functors.FalsePredicate;
  24. import org.apache.commons.collections.functors.IdentityPredicate;
  25. import org.apache.commons.collections.functors.InstanceofPredicate;
  26. import org.apache.commons.collections.functors.InvokerTransformer;
  27. import org.apache.commons.collections.functors.NonePredicate;
  28. import org.apache.commons.collections.functors.NotNullPredicate;
  29. import org.apache.commons.collections.functors.NotPredicate;
  30. import org.apache.commons.collections.functors.NullIsExceptionPredicate;
  31. import org.apache.commons.collections.functors.NullIsFalsePredicate;
  32. import org.apache.commons.collections.functors.NullIsTruePredicate;
  33. import org.apache.commons.collections.functors.NullPredicate;
  34. import org.apache.commons.collections.functors.OnePredicate;
  35. import org.apache.commons.collections.functors.OrPredicate;
  36. import org.apache.commons.collections.functors.TransformedPredicate;
  37. import org.apache.commons.collections.functors.TransformerPredicate;
  38. import org.apache.commons.collections.functors.TruePredicate;
  39. import org.apache.commons.collections.functors.UniquePredicate;
  40. /**
  41. * <code>PredicateUtils</code> provides reference implementations and utilities
  42. * for the Predicate functor interface. The supplied predicates are:
  43. * <ul>
  44. * <li>Invoker - returns the result of a method call on the input object
  45. * <li>InstanceOf - true if the object is an instanceof a class
  46. * <li>Equal - true if the object equals() a specified object
  47. * <li>Identity - true if the object == a specified object
  48. * <li>Null - true if the object is null
  49. * <li>NotNull - true if the object is not null
  50. * <li>Unique - true if the object has not already been evaluated
  51. * <li>And/All - true if all of the predicates are true
  52. * <li>Or/Any - true if any of the predicates is true
  53. * <li>Either/One - true if only one of the predicate is true
  54. * <li>Neither/None - true if none of the predicates are true
  55. * <li>Not - true if the predicate is false, and vice versa
  56. * <li>Transformer - wraps a Transformer as a Predicate
  57. * <li>True - always return true
  58. * <li>False - always return false
  59. * <li>Exception - always throws an exception
  60. * <li>NullIsException/NullIsFalse/NullIsTrue - check for null input
  61. * <li>Transformed - transforms the input before calling the predicate
  62. * </ul>
  63. * All the supplied predicates are Serializable.
  64. *
  65. * @since Commons Collections 3.0
  66. * @version $Revision: 1.19 $ $Date: 2004/05/26 21:50:52 $
  67. *
  68. * @author Stephen Colebourne
  69. * @author Ola Berg
  70. */
  71. public class PredicateUtils {
  72. /**
  73. * This class is not normally instantiated.
  74. */
  75. public PredicateUtils() {
  76. super();
  77. }
  78. // Simple predicates
  79. //-----------------------------------------------------------------------------
  80. /**
  81. * Gets a Predicate that always throws an exception.
  82. * This could be useful during testing as a placeholder.
  83. *
  84. * @see org.apache.commons.collections.functors.ExceptionPredicate
  85. *
  86. * @return the predicate
  87. */
  88. public static Predicate exceptionPredicate() {
  89. return ExceptionPredicate.INSTANCE;
  90. }
  91. /**
  92. * Gets a Predicate that always returns true.
  93. *
  94. * @see org.apache.commons.collections.functors.TruePredicate
  95. *
  96. * @return the predicate
  97. */
  98. public static Predicate truePredicate() {
  99. return TruePredicate.INSTANCE;
  100. }
  101. /**
  102. * Gets a Predicate that always returns false.
  103. *
  104. * @see org.apache.commons.collections.functors.FalsePredicate
  105. *
  106. * @return the predicate
  107. */
  108. public static Predicate falsePredicate() {
  109. return FalsePredicate.INSTANCE;
  110. }
  111. /**
  112. * Gets a Predicate that checks if the input object passed in is null.
  113. *
  114. * @see org.apache.commons.collections.functors.NullPredicate
  115. *
  116. * @return the predicate
  117. */
  118. public static Predicate nullPredicate() {
  119. return NullPredicate.INSTANCE;
  120. }
  121. /**
  122. * Gets a Predicate that checks if the input object passed in is not null.
  123. *
  124. * @see org.apache.commons.collections.functors.NotNullPredicate
  125. *
  126. * @return the predicate
  127. */
  128. public static Predicate notNullPredicate() {
  129. return NotNullPredicate.INSTANCE;
  130. }
  131. /**
  132. * Creates a Predicate that checks if the input object is equal to the
  133. * specified object using equals().
  134. *
  135. * @see org.apache.commons.collections.functors.EqualPredicate
  136. *
  137. * @param value the value to compare against
  138. * @return the predicate
  139. */
  140. public static Predicate equalPredicate(Object value) {
  141. return EqualPredicate.getInstance(value);
  142. }
  143. /**
  144. * Creates a Predicate that checks if the input object is equal to the
  145. * specified object by identity.
  146. *
  147. * @see org.apache.commons.collections.functors.IdentityPredicate
  148. *
  149. * @param value the value to compare against
  150. * @return the predicate
  151. */
  152. public static Predicate identityPredicate(Object value) {
  153. return IdentityPredicate.getInstance(value);
  154. }
  155. /**
  156. * Creates a Predicate that checks if the object passed in is of
  157. * a particular type, using instanceof. A <code>null</code> input
  158. * object will return <code>false</code>.
  159. *
  160. * @see org.apache.commons.collections.functors.InstanceofPredicate
  161. *
  162. * @param type the type to check for, may not be null
  163. * @return the predicate
  164. * @throws IllegalArgumentException if the class is null
  165. */
  166. public static Predicate instanceofPredicate(Class type) {
  167. return InstanceofPredicate.getInstance(type);
  168. }
  169. /**
  170. * Creates a Predicate that returns true the first time an object is
  171. * encountered, and false if the same object is received
  172. * again. The comparison is by equals(). A <code>null</code> input object
  173. * is accepted and will return true the first time, and false subsequently
  174. * as well.
  175. *
  176. * @see org.apache.commons.collections.functors.UniquePredicate
  177. *
  178. * @return the predicate
  179. */
  180. public static Predicate uniquePredicate() {
  181. // must return new instance each time
  182. return UniquePredicate.getInstance();
  183. }
  184. /**
  185. * Creates a Predicate that invokes a method on the input object.
  186. * The method must return either a boolean or a non-null Boolean,
  187. * and have no parameters. If the input object is null, a
  188. * PredicateException is thrown.
  189. * <p>
  190. * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
  191. * will call the <code>isEmpty</code> method on the input object to
  192. * determine the predicate result.
  193. *
  194. * @see org.apache.commons.collections.functors.InvokerTransformer
  195. * @see org.apache.commons.collections.functors.TransformerPredicate
  196. *
  197. * @param methodName the method name to call on the input object, may not be null
  198. * @return the predicate
  199. * @throws IllegalArgumentException if the methodName is null.
  200. */
  201. public static Predicate invokerPredicate(String methodName){
  202. // reuse transformer as it has caching - this is lazy really, should have inner class here
  203. return asPredicate(InvokerTransformer.getInstance(methodName));
  204. }
  205. /**
  206. * Creates a Predicate that invokes a method on the input object.
  207. * The method must return either a boolean or a non-null Boolean,
  208. * and have no parameters. If the input object is null, a
  209. * PredicateException is thrown.
  210. * <p>
  211. * For example, <code>PredicateUtils.invokerPredicate("isEmpty");</code>
  212. * will call the <code>isEmpty</code> method on the input object to
  213. * determine the predicate result.
  214. *
  215. * @see org.apache.commons.collections.functors.InvokerTransformer
  216. * @see org.apache.commons.collections.functors.TransformerPredicate
  217. *
  218. * @param methodName the method name to call on the input object, may not be null
  219. * @param paramTypes the parameter types
  220. * @param args the arguments
  221. * @return the predicate
  222. * @throws IllegalArgumentException if the method name is null
  223. * @throws IllegalArgumentException if the paramTypes and args don't match
  224. */
  225. public static Predicate invokerPredicate(String methodName, Class[] paramTypes, Object[] args){
  226. // reuse transformer as it has caching - this is lazy really, should have inner class here
  227. return asPredicate(InvokerTransformer.getInstance(methodName, paramTypes, args));
  228. }
  229. // Boolean combinations
  230. //-----------------------------------------------------------------------------
  231. /**
  232. * Create a new Predicate that returns true only if both of the specified
  233. * predicates are true.
  234. *
  235. * @see org.apache.commons.collections.functors.AndPredicate
  236. *
  237. * @param predicate1 the first predicate, may not be null
  238. * @param predicate2 the second predicate, may not be null
  239. * @return the <code>and</code> predicate
  240. * @throws IllegalArgumentException if either predicate is null
  241. */
  242. public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) {
  243. return AndPredicate.getInstance(predicate1, predicate2);
  244. }
  245. /**
  246. * Create a new Predicate that returns true only if all of the specified
  247. * predicates are true.
  248. *
  249. * @see org.apache.commons.collections.functors.AllPredicate
  250. *
  251. * @param predicates an array of predicates to check, may not be null
  252. * @return the <code>all</code> predicate
  253. * @throws IllegalArgumentException if the predicates array is null
  254. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  255. * @throws IllegalArgumentException if any predicate in the array is null
  256. */
  257. public static Predicate allPredicate(Predicate[] predicates) {
  258. return AllPredicate.getInstance(predicates);
  259. }
  260. /**
  261. * Create a new Predicate that returns true only if all of the specified
  262. * predicates are true. The predicates are checked in iterator order.
  263. *
  264. * @see org.apache.commons.collections.functors.AllPredicate
  265. *
  266. * @param predicates a collection of predicates to check, may not be null
  267. * @return the <code>all</code> predicate
  268. * @throws IllegalArgumentException if the predicates collection is null
  269. * @throws IllegalArgumentException if the predicates collection has less than 2 elements
  270. * @throws IllegalArgumentException if any predicate in the collection is null
  271. */
  272. public static Predicate allPredicate(Collection predicates) {
  273. return AllPredicate.getInstance(predicates);
  274. }
  275. /**
  276. * Create a new Predicate that returns true if either of the specified
  277. * predicates are true.
  278. *
  279. * @see org.apache.commons.collections.functors.OrPredicate
  280. *
  281. * @param predicate1 the first predicate, may not be null
  282. * @param predicate2 the second predicate, may not be null
  283. * @return the <code>or</code> predicate
  284. * @throws IllegalArgumentException if either predicate is null
  285. */
  286. public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) {
  287. return OrPredicate.getInstance(predicate1, predicate2);
  288. }
  289. /**
  290. * Create a new Predicate that returns true if any of the specified
  291. * predicates are true.
  292. *
  293. * @see org.apache.commons.collections.functors.AnyPredicate
  294. *
  295. * @param predicates an array of predicates to check, may not be null
  296. * @return the <code>any</code> predicate
  297. * @throws IllegalArgumentException if the predicates array is null
  298. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  299. * @throws IllegalArgumentException if any predicate in the array is null
  300. */
  301. public static Predicate anyPredicate(Predicate[] predicates) {
  302. return AnyPredicate.getInstance(predicates);
  303. }
  304. /**
  305. * Create a new Predicate that returns true if any of the specified
  306. * predicates are true. The predicates are checked in iterator order.
  307. *
  308. * @see org.apache.commons.collections.functors.AnyPredicate
  309. *
  310. * @param predicates a collection of predicates to check, may not be null
  311. * @return the <code>any</code> predicate
  312. * @throws IllegalArgumentException if the predicates collection is null
  313. * @throws IllegalArgumentException if the predicates collection has less than 2 elements
  314. * @throws IllegalArgumentException if any predicate in the collection is null
  315. */
  316. public static Predicate anyPredicate(Collection predicates) {
  317. return AnyPredicate.getInstance(predicates);
  318. }
  319. /**
  320. * Create a new Predicate that returns true if one, but not both, of the
  321. * specified predicates are true.
  322. *
  323. * @see org.apache.commons.collections.functors.OnePredicate
  324. *
  325. * @param predicate1 the first predicate, may not be null
  326. * @param predicate2 the second predicate, may not be null
  327. * @return the <code>either</code> predicate
  328. * @throws IllegalArgumentException if either predicate is null
  329. */
  330. public static Predicate eitherPredicate(Predicate predicate1, Predicate predicate2) {
  331. return onePredicate(new Predicate[] { predicate1, predicate2 });
  332. }
  333. /**
  334. * Create a new Predicate that returns true if only one of the specified
  335. * predicates are true.
  336. *
  337. * @see org.apache.commons.collections.functors.OnePredicate
  338. *
  339. * @param predicates an array of predicates to check, may not be null
  340. * @return the <code>one</code> predicate
  341. * @throws IllegalArgumentException if the predicates array is null
  342. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  343. * @throws IllegalArgumentException if any predicate in the array is null
  344. */
  345. public static Predicate onePredicate(Predicate[] predicates) {
  346. return OnePredicate.getInstance(predicates);
  347. }
  348. /**
  349. * Create a new Predicate that returns true if only one of the specified
  350. * predicates are true. The predicates are checked in iterator order.
  351. *
  352. * @see org.apache.commons.collections.functors.OnePredicate
  353. *
  354. * @param predicates a collection of predicates to check, may not be null
  355. * @return the <code>one</code> predicate
  356. * @throws IllegalArgumentException if the predicates collection is null
  357. * @throws IllegalArgumentException if the predicates collection has less than 2 elements
  358. * @throws IllegalArgumentException if any predicate in the collection is null
  359. */
  360. public static Predicate onePredicate(Collection predicates) {
  361. return OnePredicate.getInstance(predicates);
  362. }
  363. /**
  364. * Create a new Predicate that returns true if neither of the specified
  365. * predicates are true.
  366. *
  367. * @see org.apache.commons.collections.functors.NonePredicate
  368. *
  369. * @param predicate1 the first predicate, may not be null
  370. * @param predicate2 the second predicate, may not be null
  371. * @return the <code>neither</code> predicate
  372. * @throws IllegalArgumentException if either predicate is null
  373. */
  374. public static Predicate neitherPredicate(Predicate predicate1, Predicate predicate2) {
  375. return nonePredicate(new Predicate[] { predicate1, predicate2 });
  376. }
  377. /**
  378. * Create a new Predicate that returns true if none of the specified
  379. * predicates are true.
  380. *
  381. * @see org.apache.commons.collections.functors.NonePredicate
  382. *
  383. * @param predicates an array of predicates to check, may not be null
  384. * @return the <code>none</code> predicate
  385. * @throws IllegalArgumentException if the predicates array is null
  386. * @throws IllegalArgumentException if the predicates array has less than 2 elements
  387. * @throws IllegalArgumentException if any predicate in the array is null
  388. */
  389. public static Predicate nonePredicate(Predicate[] predicates) {
  390. return NonePredicate.getInstance(predicates);
  391. }
  392. /**
  393. * Create a new Predicate that returns true if none of the specified
  394. * predicates are true. The predicates are checked in iterator order.
  395. *
  396. * @see org.apache.commons.collections.functors.NonePredicate
  397. *
  398. * @param predicates a collection of predicates to check, may not be null
  399. * @return the <code>none</code> predicate
  400. * @throws IllegalArgumentException if the predicates collection is null
  401. * @throws IllegalArgumentException if the predicates collection has less than 2 elements
  402. * @throws IllegalArgumentException if any predicate in the collection is null
  403. */
  404. public static Predicate nonePredicate(Collection predicates) {
  405. return NonePredicate.getInstance(predicates);
  406. }
  407. /**
  408. * Create a new Predicate that returns true if the specified predicate
  409. * returns false and vice versa.
  410. *
  411. * @see org.apache.commons.collections.functors.NotPredicate
  412. *
  413. * @param predicate the predicate to not
  414. * @return the <code>not</code> predicate
  415. * @throws IllegalArgumentException if the predicate is null
  416. */
  417. public static Predicate notPredicate(Predicate predicate) {
  418. return NotPredicate.getInstance(predicate);
  419. }
  420. // Adaptors
  421. //-----------------------------------------------------------------------------
  422. /**
  423. * Create a new Predicate that wraps a Transformer. The Transformer must
  424. * return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException
  425. * will be thrown.
  426. *
  427. * @see org.apache.commons.collections.functors.TransformerPredicate
  428. *
  429. * @param transformer the transformer to wrap, may not be null
  430. * @return the transformer wrapping predicate
  431. * @throws IllegalArgumentException if the transformer is null
  432. */
  433. public static Predicate asPredicate(Transformer transformer) {
  434. return TransformerPredicate.getInstance(transformer);
  435. }
  436. // Null handlers
  437. //-----------------------------------------------------------------------------
  438. /**
  439. * Gets a Predicate that throws an exception if the input object is null,
  440. * otherwise it calls the specified Predicate. This allows null handling
  441. * behaviour to be added to Predicates that don't support nulls.
  442. *
  443. * @see org.apache.commons.collections.functors.NullIsExceptionPredicate
  444. *
  445. * @param predicate the predicate to wrap, may not be null
  446. * @return the predicate
  447. * @throws IllegalArgumentException if the predicate is null.
  448. */
  449. public static Predicate nullIsExceptionPredicate(Predicate predicate){
  450. return NullIsExceptionPredicate.getInstance(predicate);
  451. }
  452. /**
  453. * Gets a Predicate that returns false if the input object is null, otherwise
  454. * it calls the specified Predicate. This allows null handling behaviour to
  455. * be added to Predicates that don't support nulls.
  456. *
  457. * @see org.apache.commons.collections.functors.NullIsFalsePredicate
  458. *
  459. * @param predicate the predicate to wrap, may not be null
  460. * @return the predicate
  461. * @throws IllegalArgumentException if the predicate is null.
  462. */
  463. public static Predicate nullIsFalsePredicate(Predicate predicate){
  464. return NullIsFalsePredicate.getInstance(predicate);
  465. }
  466. /**
  467. * Gets a Predicate that returns true if the input object is null, otherwise
  468. * it calls the specified Predicate. This allows null handling behaviour to
  469. * be added to Predicates that don't support nulls.
  470. *
  471. * @see org.apache.commons.collections.functors.NullIsTruePredicate
  472. *
  473. * @param predicate the predicate to wrap, may not be null
  474. * @return the predicate
  475. * @throws IllegalArgumentException if the predicate is null.
  476. */
  477. public static Predicate nullIsTruePredicate(Predicate predicate){
  478. return NullIsTruePredicate.getInstance(predicate);
  479. }
  480. // Transformed
  481. //-----------------------------------------------------------------------
  482. /**
  483. * Creates a predicate that transforms the input object before passing it
  484. * to the predicate.
  485. *
  486. * @see org.apache.commons.collections.functors.TransformedPredicate
  487. *
  488. * @param transformer the transformer to call first
  489. * @param predicate the predicate to call with the result of the transform
  490. * @return the predicate
  491. * @throws IllegalArgumentException if the transformer or the predicate is null
  492. * @since Commons Collections 3.1
  493. */
  494. public static Predicate transformedPredicate(Transformer transformer, Predicate predicate) {
  495. return TransformedPredicate.getInstance(transformer, predicate);
  496. }
  497. }