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    *      http://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.collections.buffer;
18  
19  import org.apache.commons.collections.Buffer;
20  import org.apache.commons.collections.Predicate;
21  import org.apache.commons.collections.collection.PredicatedCollection;
22  
23  /**
24   * Decorates another {@link Buffer} to validate that additions
25   * match a specified predicate.
26   * <p>
27   * This buffer exists to provide validation for the decorated buffer.
28   * It is normally created to decorate an empty buffer.
29   * If an object cannot be added to the buffer, an IllegalArgumentException is thrown.
30   * <p>
31   * One usage would be to ensure that no null entries are added to the buffer.
32   * <pre>Buffer buffer = PredicatedBuffer.decorate(new UnboundedFifoBuffer(), NotNullPredicate.INSTANCE);</pre>
33   * <p>
34   * This class is Serializable from Commons Collections 3.1.
35   *
36   * @since 3.0
37   * @version $Id: PredicatedBuffer.java 1436835 2013-01-22 11:03:31Z tn $
38   */
39  public class PredicatedBuffer<E> extends PredicatedCollection<E> implements Buffer<E> {
40  
41      /** Serialization version */
42      private static final long serialVersionUID = 2307609000539943581L;
43  
44      /**
45       * Factory method to create a predicated (validating) buffer.
46       * <p>
47       * If there are any elements already in the buffer being decorated, they
48       * are validated.
49       * 
50       * @param <E> the type of the elements in the buffer
51       * @param buffer  the buffer to decorate, must not be null
52       * @param predicate  the predicate to use for validation, must not be null
53       * @return a new predicated Buffer
54       * @throws IllegalArgumentException if buffer or predicate is null
55       * @throws IllegalArgumentException if the buffer contains invalid elements
56       */
57      public static <E> PredicatedBuffer<E> predicatedBuffer(final Buffer<E> buffer,
58                                                             final Predicate<? super E> predicate) {
59          return new PredicatedBuffer<E>(buffer, predicate);
60      }
61      
62      //-----------------------------------------------------------------------
63      /**
64       * Constructor that wraps (not copies).
65       * <p>
66       * If there are any elements already in the collection being decorated, they
67       * are validated.
68       * 
69       * @param buffer  the buffer to decorate, must not be null
70       * @param predicate  the predicate to use for validation, must not be null
71       * @throws IllegalArgumentException if buffer or predicate is null
72       * @throws IllegalArgumentException if the buffer contains invalid elements
73       */
74      protected PredicatedBuffer(final Buffer<E> buffer, final Predicate<? super E> predicate) {
75          super(buffer, predicate);
76      }
77  
78      /**
79       * Gets the buffer being decorated.
80       * 
81       * @return the decorated buffer
82       */
83      @Override
84      protected Buffer<E> decorated() {
85          return (Buffer<E>) super.decorated();
86      }
87  
88      //-----------------------------------------------------------------------
89      
90      public E get() {
91          return decorated().get();
92      }
93  
94      public E remove() {
95          return decorated().remove();
96      }
97  
98  }