001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections.buffer; 018 019 import org.apache.commons.collections.Buffer; 020 import org.apache.commons.collections.Predicate; 021 import org.apache.commons.collections.collection.PredicatedCollection; 022 023 /** 024 * Decorates another <code>Buffer</code> to validate that additions 025 * match a specified predicate. 026 * <p> 027 * This buffer exists to provide validation for the decorated buffer. 028 * It is normally created to decorate an empty buffer. 029 * If an object cannot be added to the buffer, an IllegalArgumentException is thrown. 030 * <p> 031 * One usage would be to ensure that no null entries are added to the buffer. 032 * <pre>Buffer buffer = PredicatedBuffer.decorate(new UnboundedFifoBuffer(), NotNullPredicate.INSTANCE);</pre> 033 * <p> 034 * This class is Serializable from Commons Collections 3.1. 035 * 036 * @since Commons Collections 3.0 037 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 038 * 039 * @author Stephen Colebourne 040 * @author Paul Jack 041 */ 042 public class PredicatedBuffer extends PredicatedCollection implements Buffer { 043 044 /** Serialization version */ 045 private static final long serialVersionUID = 2307609000539943581L; 046 047 /** 048 * Factory method to create a predicated (validating) buffer. 049 * <p> 050 * If there are any elements already in the buffer being decorated, they 051 * are validated. 052 * 053 * @param buffer the buffer to decorate, must not be null 054 * @param predicate the predicate to use for validation, must not be null 055 * @return a new predicated Buffer 056 * @throws IllegalArgumentException if buffer or predicate is null 057 * @throws IllegalArgumentException if the buffer contains invalid elements 058 */ 059 public static Buffer decorate(Buffer buffer, Predicate predicate) { 060 return new PredicatedBuffer(buffer, predicate); 061 } 062 063 //----------------------------------------------------------------------- 064 /** 065 * Constructor that wraps (not copies). 066 * <p> 067 * If there are any elements already in the collection being decorated, they 068 * are validated. 069 * 070 * @param buffer the buffer to decorate, must not be null 071 * @param predicate the predicate to use for validation, must not be null 072 * @throws IllegalArgumentException if buffer or predicate is null 073 * @throws IllegalArgumentException if the buffer contains invalid elements 074 */ 075 protected PredicatedBuffer(Buffer buffer, Predicate predicate) { 076 super(buffer, predicate); 077 } 078 079 /** 080 * Gets the buffer being decorated. 081 * 082 * @return the decorated buffer 083 */ 084 protected Buffer getBuffer() { 085 return (Buffer) getCollection(); 086 } 087 088 //----------------------------------------------------------------------- 089 public Object get() { 090 return getBuffer().get(); 091 } 092 093 public Object remove() { 094 return getBuffer().remove(); 095 } 096 097 }