1 /** 2 * Copyright 2005-2015 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.rice.krad.data; 17 18 import java.io.Serializable; 19 import java.util.Collections; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 /** 24 * Stores the values for a multi-valued key. This is intended primarily for use on 25 * {@link DataObjectService#find(Class, Object)} in situations where you have a data object which has a compound 26 * primary key represented by more than one field. In such cases the keys in the map you construction this class with 27 * should be the field names of the primary key fields, and the values in the maps should be the values by which you 28 * want to perform the find. 29 * 30 * @author Kuali Rice Team (rice.collab@kuali.org) 31 */ 32 public final class CompoundKey implements Serializable { 33 34 private static final long serialVersionUID = 1L; 35 36 private final Map<String, ?> keys; 37 38 /** 39 * Construct a new instance of a CompoundKey from the given key values map. 40 * 41 * @param keys map of field name to value for the compound key, must be non-null and non-empty 42 * 43 * @throws IllegalArgumentException if the given Map is null or empty 44 */ 45 public CompoundKey(Map<String, ?> keys) { 46 if (keys == null || keys.isEmpty()) { 47 throw new IllegalArgumentException("Compound key map should be non-null as well as having at least one" 48 + "value."); 49 } 50 this.keys = new HashMap<String, Object>(keys); 51 } 52 53 /** 54 * Returns an unmodifable Map of the key values on this CompoundKey 55 * 56 * @return unmodifiable map of the key values on this CompoundKey 57 */ 58 public Map<String, ?> getKeys() { 59 return Collections.unmodifiableMap(keys); 60 } 61 62 /** 63 * Returns true if any of the fields in this compound key have null values, since that usually indicates an 64 * incomplete and unsaved object. 65 * 66 * @return 67 */ 68 public boolean hasNullKeyValues() { 69 for (Object value : keys.values()) { 70 if (value == null) { 71 return true; 72 } 73 } 74 return false; 75 } 76 77 }