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.collections4.bag; 18 19 import java.util.Comparator; 20 21 import org.apache.commons.collections4.Bag; 22 import org.apache.commons.collections4.SortedBag; 23 24 /** 25 * Decorates another {@link SortedBag} to synchronize its behaviour 26 * for a multi-threaded environment. 27 * <p> 28 * Methods are synchronized, then forwarded to the decorated bag. 29 * Iterators must be separately synchronized around the loop. 30 * </p> 31 * <p> 32 * This class is Serializable from Commons Collections 3.1. 33 * </p> 34 * 35 * @param <E> the type of elements in this bag 36 * @since 3.0 37 */ 38 public class SynchronizedSortedBag<E> extends SynchronizedBag<E> implements SortedBag<E> { 39 40 /** Serialization version */ 41 private static final long serialVersionUID = 722374056718497858L; 42 43 /** 44 * Factory method to create a synchronized sorted bag. 45 * 46 * @param <E> the type of the elements in the bag 47 * @param bag the bag to decorate, must not be null 48 * @return a new synchronized SortedBag 49 * @throws NullPointerException if bag is null 50 * @since 4.0 51 */ 52 public static <E> SynchronizedSortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) { 53 return new SynchronizedSortedBag<>(bag); 54 } 55 56 //----------------------------------------------------------------------- 57 /** 58 * Constructor that wraps (not copies). 59 * 60 * @param bag the bag to decorate, must not be null 61 * @throws NullPointerException if bag is null 62 */ 63 protected SynchronizedSortedBag(final SortedBag<E> bag) { 64 super(bag); 65 } 66 67 /** 68 * Constructor that wraps (not copies). 69 * 70 * @param bag the bag to decorate, must not be null 71 * @param lock the lock to use, must not be null 72 * @throws NullPointerException if bag or lock is null 73 */ 74 protected SynchronizedSortedBag(final Bag<E> bag, final Object lock) { 75 super(bag, lock); 76 } 77 78 /** 79 * Gets the bag being decorated. 80 * 81 * @return the decorated bag 82 */ 83 protected SortedBag<E> getSortedBag() { 84 return (SortedBag<E>) decorated(); 85 } 86 87 //----------------------------------------------------------------------- 88 89 @Override 90 public synchronized E first() { 91 synchronized (lock) { 92 return getSortedBag().first(); 93 } 94 } 95 96 @Override 97 public synchronized E last() { 98 synchronized (lock) { 99 return getSortedBag().last(); 100 } 101 } 102 103 @Override 104 public synchronized Comparator<? super E> comparator() { 105 synchronized (lock) { 106 return getSortedBag().comparator(); 107 } 108 } 109 110 }