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 behavior 026 * for a multithreaded 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 * <p> 032 * This class is Serializable from Commons Collections 3.1. 033 * </p> 034 * 035 * @param <E> the type of elements in this bag 036 * @since 3.0 037 */ 038public class SynchronizedSortedBag<E> extends SynchronizedBag<E> implements SortedBag<E> { 039 040 /** Serialization version */ 041 private static final long serialVersionUID = 722374056718497858L; 042 043 /** 044 * Factory method to create a synchronized sorted bag. 045 * 046 * @param <E> the type of the elements in the bag 047 * @param bag the bag to decorate, must not be null 048 * @return a new synchronized SortedBag 049 * @throws NullPointerException if bag is null 050 * @since 4.0 051 */ 052 public static <E> SynchronizedSortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) { 053 return new SynchronizedSortedBag<>(bag); 054 } 055 056 /** 057 * Constructor that wraps (not copies). 058 * 059 * @param bag the bag to decorate, must not be null 060 * @param lock the lock to use, must not be null 061 * @throws NullPointerException if bag or lock is null 062 */ 063 protected SynchronizedSortedBag(final Bag<E> bag, final Object lock) { 064 super(bag, lock); 065 } 066 067 /** 068 * Constructor that wraps (not copies). 069 * 070 * @param bag the bag to decorate, must not be null 071 * @throws NullPointerException if bag is null 072 */ 073 protected SynchronizedSortedBag(final SortedBag<E> bag) { 074 super(bag); 075 } 076 077 @Override 078 public synchronized Comparator<? super E> comparator() { 079 synchronized (lock) { 080 return getSortedBag().comparator(); 081 } 082 } 083 084 @Override 085 public synchronized E first() { 086 synchronized (lock) { 087 return getSortedBag().first(); 088 } 089 } 090 091 /** 092 * Gets the bag being decorated. 093 * 094 * @return the decorated bag 095 */ 096 protected SortedBag<E> getSortedBag() { 097 return (SortedBag<E>) decorated(); 098 } 099 100 @Override 101 public synchronized E last() { 102 synchronized (lock) { 103 return getSortedBag().last(); 104 } 105 } 106 107}