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.multiset; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.io.Serializable; 023import java.util.Collection; 024import java.util.Comparator; 025import java.util.Objects; 026import java.util.SortedMap; 027import java.util.TreeMap; 028 029import org.apache.commons.collections4.SortedMultiSet; 030 031/** 032 * Implements {@link SortedMultiSet}, using a {@link TreeMap} to provide the 033 * data storage. This is the standard implementation of a sorted multiset. 034 * <p> 035 * Order will be maintained among the multiset members and can be viewed 036 * through the iterator. 037 * </p> 038 * <p> 039 * A {@code MultiSet} stores each object in the collection together with a 040 * count of occurrences. Extra methods on the interface allow multiple copies 041 * of an object to be added or removed at once. 042 * </p> 043 * <p> 044 * <strong>Note that TreeMultiSet is not synchronized and is not thread-safe.</strong> 045 * If you wish to use this multiset from multiple threads concurrently, you must use 046 * appropriate synchronization. The simplest approach is to wrap this multiset using 047 * {@link org.apache.commons.collections4.MultiSetUtils#synchronizedSortedMultiSet(SortedMultiSet)}. 048 * Unsynchronized concurrent modification can corrupt the structure of the backing 049 * {@link TreeMap}, and a malformed tree may cause subsequent operations, including 050 * reads, to enter an infinite loop. 051 * </p> 052 * 053 * @param <E> The type held in the multiset 054 * @since 4.6.0 055 */ 056public class TreeMultiSet<E> extends AbstractMapMultiSet<E> implements SortedMultiSet<E>, Serializable { 057 058 /** Serial version lock */ 059 private static final long serialVersionUID = 20260705L; 060 061 /** 062 * Constructs an empty {@link TreeMultiSet}. 063 */ 064 public TreeMultiSet() { 065 super(new TreeMap<>()); 066 } 067 068 /** 069 * Constructs a {@link TreeMultiSet} containing all the members of the 070 * specified collection. 071 * 072 * @param coll The collection to copy into the multiset 073 */ 074 public TreeMultiSet(final Collection<? extends E> coll) { 075 this(); 076 addAll(coll); 077 } 078 079 /** 080 * Constructs an empty multiset that maintains order on its unique representative 081 * members according to the given {@link Comparator}. 082 * 083 * @param comparator The comparator to use 084 */ 085 public TreeMultiSet(final Comparator<? super E> comparator) { 086 super(new TreeMap<>(comparator)); 087 } 088 089 /** 090 * Constructs a multiset containing all the members of the given Iterable. 091 * 092 * @param iterable An iterable to copy into this multiset. 093 * @since 4.6.0 094 */ 095 public TreeMultiSet(final Iterable<? extends E> iterable) { 096 super(new TreeMap<>(), iterable); 097 } 098 099 /** 100 * {@inheritDoc} 101 * 102 * @throws IllegalArgumentException if the object to be added does not implement 103 * {@link Comparable} and the {@link TreeMultiSet} is using natural ordering 104 * @throws NullPointerException if the specified key is null and this multiset uses 105 * natural ordering, or its comparator does not permit null keys 106 */ 107 @Override 108 public int add(final E object, final int occurrences) { 109 if (comparator() == null && !(object instanceof Comparable)) { 110 Objects.requireNonNull(object, "object"); 111 throw new IllegalArgumentException("Objects of type " + object.getClass() + " cannot be added to " + 112 "a naturally ordered TreeMultiSet as it does not implement Comparable"); 113 } 114 return super.add(object, occurrences); 115 } 116 117 @Override 118 public Comparator<? super E> comparator() { 119 return getMap().comparator(); 120 } 121 122 @Override 123 public E first() { 124 return getMap().firstKey(); 125 } 126 127 @Override 128 protected SortedMap<E, AbstractMapMultiSet.MutableInteger> getMap() { 129 return (SortedMap<E, AbstractMapMultiSet.MutableInteger>) super.getMap(); 130 } 131 132 @Override 133 public E last() { 134 return getMap().lastKey(); 135 } 136 137 /** 138 * Deserializes the multiset in using a custom routine. 139 * 140 * @param in The input stream 141 * @throws IOException Thrown if an error occurs while reading from the stream 142 * @throws ClassNotFoundException if an object read from the stream cannot be loaded 143 */ 144 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 145 in.defaultReadObject(); 146 @SuppressWarnings("unchecked") // This will fail at runtime if the stream is incorrect 147 final Comparator<? super E> comp = (Comparator<? super E>) in.readObject(); 148 setMap(new TreeMap<>(comp)); 149 super.doReadObject(in); 150 } 151 152 /** 153 * Serializes this object to an ObjectOutputStream. 154 * 155 * @param out The target ObjectOutputStream. 156 * @throws IOException thrown when an I/O errors occur writing to the target stream. 157 */ 158 private void writeObject(final ObjectOutputStream out) throws IOException { 159 out.defaultWriteObject(); 160 out.writeObject(comparator()); 161 super.doWriteObject(out); 162 } 163 164}