View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.bag;
18  
19  import java.util.Comparator;
20  
21  import org.apache.commons.collections4.Predicate;
22  import org.apache.commons.collections4.SortedBag;
23  import org.apache.commons.collections4.multiset.PredicatedSortedMultiSet;
24  
25  /**
26   * Decorates another {@link SortedBag} to validate that additions
27   * match a specified predicate.
28   * <p>
29   * This bag exists to provide validation for the decorated bag.
30   * It is normally created to decorate an empty bag.
31   * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown.
32   * </p>
33   * <p>
34   * One usage would be to ensure that no null entries are added to the bag.
35   * <pre>
36   * SortedBag bag = PredicatedSortedBag.predicatedSortedBag(new TreeBag(), NotNullPredicate.INSTANCE);
37   * </pre>
38   * <p>
39   * This class is Serializable from Commons Collections 3.1.
40   * </p>
41   *
42   * @param <E> The type of elements in this bag
43   * @since 3.0
44   * @deprecated Since 4.6.0, use {@link PredicatedSortedMultiSet} instead.
45   */
46  @Deprecated
47  public class PredicatedSortedBag<E> extends PredicatedBag<E> implements SortedBag<E> {
48  
49      /** Serialization version */
50      private static final long serialVersionUID = 3448581314086406616L;
51  
52      /**
53       * Factory method to create a predicated (validating) bag.
54       * <p>
55       * If there are any elements already in the bag being decorated, they
56       * are validated.
57       *
58       * @param <E> The type of the elements in the bag
59       * @param bag  The bag to decorate, must not be null
60       * @param predicate  The predicate to use for validation, must not be null
61       * @return A new predicated SortedBag
62       * @throws NullPointerException if bag or predicate is null
63       * @throws IllegalArgumentException if the bag contains invalid elements
64       * @since 4.0
65       */
66      public static <E> PredicatedSortedBag<E> predicatedSortedBag(final SortedBag<E> bag,
67                                                                   final Predicate<? super E> predicate) {
68          return new PredicatedSortedBag<>(bag, predicate);
69      }
70  
71      /**
72       * Constructor that wraps (not copies).
73       * <p>If there are any elements already in the bag being decorated, they
74       * are validated.
75       *
76       * @param bag  The bag to decorate, must not be null
77       * @param predicate  The predicate to use for validation, must not be null
78       * @throws NullPointerException if bag or predicate is null
79       * @throws IllegalArgumentException if the bag contains invalid elements
80       */
81      protected PredicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) {
82          super(bag, predicate);
83      }
84  
85      @Override
86      public Comparator<? super E> comparator() {
87          return decorated().comparator();
88      }
89  
90      /**
91       * Gets the decorated sorted bag.
92       *
93       * @return The decorated bag
94       */
95      @Override
96      protected SortedBag<E> decorated() {
97          return (SortedBag<E>) super.decorated();
98      }
99  
100     @Override
101     public E first() {
102         return decorated().first();
103     }
104 
105     @Override
106     public E last() {
107         return decorated().last();
108     }
109 
110 }