001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.krad.data; 017 018import java.io.Serializable; 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Map; 022 023/** 024 * Stores the values for a multi-valued key. This is intended primarily for use on 025 * {@link DataObjectService#find(Class, Object)} in situations where you have a data object which has a compound 026 * primary key represented by more than one field. In such cases the keys in the map you construction this class with 027 * should be the field names of the primary key fields, and the values in the maps should be the values by which you 028 * want to perform the find. 029 * 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032public final class CompoundKey implements Serializable { 033 034 private static final long serialVersionUID = 1L; 035 036 private final Map<String, ?> keys; 037 038 /** 039 * Construct a new instance of a CompoundKey from the given key values map. 040 * 041 * @param keys map of field name to value for the compound key, must be non-null and non-empty 042 * 043 * @throws IllegalArgumentException if the given Map is null or empty 044 */ 045 public CompoundKey(Map<String, ?> keys) { 046 if (keys == null || keys.isEmpty()) { 047 throw new IllegalArgumentException("Compound key map should be non-null as well as having at least one" 048 + "value."); 049 } 050 this.keys = new HashMap<String, Object>(keys); 051 } 052 053 /** 054 * Returns an unmodifable Map of the key values on this CompoundKey 055 * 056 * @return unmodifiable map of the key values on this CompoundKey 057 */ 058 public Map<String, ?> getKeys() { 059 return Collections.unmodifiableMap(keys); 060 } 061 062 /** 063 * Returns true if any of the fields in this compound key have null values, since that usually indicates an 064 * incomplete and unsaved object. 065 * 066 * @return 067 */ 068 public boolean hasNullKeyValues() { 069 for (Object value : keys.values()) { 070 if (value == null) { 071 return true; 072 } 073 } 074 return false; 075 } 076 077}