001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| 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.conqat.lib.commons.collections;
018
019/**
020 * Comparator that compares objects based on the identifiers provided by a
021 * {@link IIdProvider}.
022 * 
023 * @author deissenb
024 */
025public class IdComparator<I extends Comparable<I>, T> extends IdentifierBasedComparatorBase<I, T> {
026
027        /** ID provider used for comparing. */
028        private final IIdProvider<I, T> idProvider;
029
030        /** Create new comparator. */
031        public IdComparator(IIdProvider<I, T> idProvider) {
032                this.idProvider = idProvider;
033        }
034
035        /**
036         * Obtain identifier from identifier provider.
037         * 
038         * @throws NullPointerException
039         *             if the id provider returns <code>null</code>.
040         */
041        @Override
042        protected I obtainIdentifier(T t) {
043                I id = idProvider.obtainId(t);
044                if (id == null) {
045                        throw new NullPointerException("Id for " + t + " is null.");
046                }
047                return id;
048        }
049}