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 */ 017package org.apache.commons.collections4.bag; 018 019import java.util.Comparator; 020 021import org.apache.commons.collections4.Bag; 022import org.apache.commons.collections4.SortedBag; 023 024/** 025 * Decorates another {@link SortedBag} to synchronize its behaviour 026 * for a multi-threaded environment. 027 * <p> 028 * Methods are synchronized, then forwarded to the decorated bag. 029 * Iterators must be separately synchronized around the loop. 030 * <p> 031 * This class is Serializable from Commons Collections 3.1. 032 * 033 * @since 3.0 034 * @version $Id: SynchronizedSortedBag.html 972421 2015-11-14 20:00:04Z tn $ 035 */ 036public class SynchronizedSortedBag<E> extends SynchronizedBag<E> implements SortedBag<E> { 037 038 /** Serialization version */ 039 private static final long serialVersionUID = 722374056718497858L; 040 041 /** 042 * Factory method to create a synchronized sorted bag. 043 * 044 * @param <E> the type of the elements in the bag 045 * @param bag the bag to decorate, must not be null 046 * @return a new synchronized SortedBag 047 * @throws IllegalArgumentException if bag is null 048 * @since 4.0 049 */ 050 public static <E> SynchronizedSortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) { 051 return new SynchronizedSortedBag<E>(bag); 052 } 053 054 //----------------------------------------------------------------------- 055 /** 056 * Constructor that wraps (not copies). 057 * 058 * @param bag the bag to decorate, must not be null 059 * @throws IllegalArgumentException if bag is null 060 */ 061 protected SynchronizedSortedBag(final SortedBag<E> bag) { 062 super(bag); 063 } 064 065 /** 066 * Constructor that wraps (not copies). 067 * 068 * @param bag the bag to decorate, must not be null 069 * @param lock the lock to use, must not be null 070 * @throws IllegalArgumentException if bag is null 071 */ 072 protected SynchronizedSortedBag(final Bag<E> bag, final Object lock) { 073 super(bag, lock); 074 } 075 076 /** 077 * Gets the bag being decorated. 078 * 079 * @return the decorated bag 080 */ 081 protected SortedBag<E> getSortedBag() { 082 return (SortedBag<E>) decorated(); 083 } 084 085 //----------------------------------------------------------------------- 086 087 public synchronized E first() { 088 synchronized (lock) { 089 return getSortedBag().first(); 090 } 091 } 092 093 public synchronized E last() { 094 synchronized (lock) { 095 return getSortedBag().last(); 096 } 097 } 098 099 public synchronized Comparator<? super E> comparator() { 100 synchronized (lock) { 101 return getSortedBag().comparator(); 102 } 103 } 104 105}