001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017–2019 microBean.
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
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.configuration.spi;
018
019import java.util.Comparator;
020import java.util.Objects;
021
022/**
023 * An {@code abstract} {@link Arbiter} that uses a {@link Comparator}
024 * in some way.
025 *
026 * @param <T> the type of object that will be compared
027 *
028 * @author <a href="https://about.me/lairdnelson"
029 * target="_parent">Laird Nelson</a>
030 */
031public abstract class ComparatorBasedArbiter<T> implements Arbiter {
032
033
034  /*
035   * Instance fields.
036   */
037
038
039  /**
040   * A {@link Comparator} used by this {@link ComparatorBasedArbiter}
041   * in some way.
042   *
043   * @see #getComparator()
044   */
045  private final Comparator<T> comparator;
046
047  
048  /*
049   * Constructors.
050   */
051
052
053  /**
054   * Creates a new {@link ComparatorBasedArbiter}.  Should never be
055   * used.
056   *
057   * @see #ComparatorBasedArbiter(Comparator)
058   *
059   * @deprecated Please use the {@link
060   * #ComparatorBasedArbiter(Comparator)} constructor instead.
061   */
062  @Deprecated
063  private ComparatorBasedArbiter() {
064    super();
065    this.comparator = null;
066  }
067
068  /**
069   * Creates a new {@link ComparatorBasedArbiter}.
070   *
071   * @param comparator the {@link Comparator} to use; must not be
072   * {@code null}
073   *
074   * @exception NullPointerException if {@code comparator} is {@code
075   * null}
076   *
077   * @see #getComparator()
078   */
079  protected ComparatorBasedArbiter(final Comparator<T> comparator) {
080    super();
081    this.comparator = Objects.requireNonNull(comparator);
082  }
083
084
085  /*
086   * Instance methods.
087   */
088
089
090  /**
091   * Returns the {@link Comparator} used by this {@link
092   * ComparatorBasedArbiter} implementation.
093   *
094   * <p>This method never returns {@code null}.</p>
095   *
096   * <p>Overrides of this method must never return {@code null}.</p>
097   *
098   * @return a non-{@code null} {@link Comparator}
099   *
100   * @see #ComparatorBasedArbiter(Comparator)
101   */
102  protected Comparator<T> getComparator() {
103    return this.comparator;
104  }
105  
106}