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