11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport javax.annotation.Nullable;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * A constraint on the keys and values that may be added to a {@code Map} or
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code Multimap}. For example, {@link MapConstraints#notNull()}, which
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * prevents a map from including any null keys or values, could be implemented
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * like this: <pre>   {@code
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   public void checkKeyValue(Object key, Object value) {
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     if (key == null || value == null) {
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       throw new NullPointerException();
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     }
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   }}</pre>
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
360888a09821a98ac0680fad765217302858e70fa4Paul Duffin * <p>In order to be effective, constraints should be deterministic; that is, they
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * should not depend on state that can change (such as external state, random
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * variables, and time) and should only depend on the value of the passed-in key
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * and value. A non-deterministic constraint cannot reliably enforce that all
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the collection's elements meet the constraint, since the constraint is only
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * enforced when elements are added.
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Mike Bostock
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @see MapConstraints
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @see Constraint
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@Beta
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic interface MapConstraint<K, V> {
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Throws a suitable {@code RuntimeException} if the specified key or value is
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * illegal. Typically this is either a {@link NullPointerException}, an
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link IllegalArgumentException}, or a {@link ClassCastException}, though
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * an application-specific exception class may be used if appropriate.
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  void checkKeyValue(@Nullable K key, @Nullable V value);
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns a brief human readable description of this constraint, such as
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * "Not null".
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
630888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  String toString();
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
66