001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.Beta; 020import com.google.common.annotations.GwtCompatible; 021 022import javax.annotation.Nullable; 023 024/** 025 * A constraint on the keys and values that may be added to a {@code Map} or 026 * {@code Multimap}. For example, {@link MapConstraints#notNull()}, which 027 * prevents a map from including any null keys or values, could be implemented 028 * like this: <pre> {@code 029 * 030 * public void checkKeyValue(Object key, Object value) { 031 * if (key == null || value == null) { 032 * throw new NullPointerException(); 033 * } 034 * }}</pre> 035 * 036 * <p>In order to be effective, constraints should be deterministic; that is, they 037 * should not depend on state that can change (such as external state, random 038 * variables, and time) and should only depend on the value of the passed-in key 039 * and value. A non-deterministic constraint cannot reliably enforce that all 040 * the collection's elements meet the constraint, since the constraint is only 041 * enforced when elements are added. 042 * 043 * @author Mike Bostock 044 * @see MapConstraints 045 * @see Constraint 046 * @since 3.0 047 * @deprecated Use {@link Preconditions} for basic checks. In place of 048 * constrained maps, we encourage you to check your preconditions 049 * explicitly instead of leaving that work to the map implementation. 050 * For the specific case of rejecting null, consider {@link ImmutableMap}. 051 * This class is scheduled for removal in Guava 20.0. 052 */ 053@GwtCompatible 054@Beta 055@Deprecated 056public interface MapConstraint<K, V> { 057 /** 058 * Throws a suitable {@code RuntimeException} if the specified key or value is 059 * illegal. Typically this is either a {@link NullPointerException}, an 060 * {@link IllegalArgumentException}, or a {@link ClassCastException}, though 061 * an application-specific exception class may be used if appropriate. 062 */ 063 void checkKeyValue(@Nullable K key, @Nullable V value); 064 065 /** 066 * Returns a brief human readable description of this constraint, such as 067 * "Not null". 068 */ 069 @Override 070 String toString(); 071}