KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > model > CompositeKey


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.model;
56
57 import java.io.Serializable JavaDoc;
58
59 import org.lateralnz.common.util.Constants;
60 import org.lateralnz.common.util.StringUtils;
61
62 /**
63  * a key 'holder' containing primary key, plus secondary key data
64  */

65 public class CompositeKey implements Serializable JavaDoc, Constants {
66   private String JavaDoc primaryKey;
67   private String JavaDoc[] secondaryKeys;
68   private String JavaDoc[] allKeys;
69   
70   public CompositeKey(String JavaDoc primaryKey, String JavaDoc[] secondaryKeys) {
71     this.primaryKey = primaryKey;
72     setSecondaryKeys(secondaryKeys);
73   }
74   
75  /**
76   * set the array of secondard keys for this composite
77   */

78   public void setSecondaryKeys(String JavaDoc[] secondaryKeys) {
79     this.secondaryKeys = secondaryKeys;
80     if (secondaryKeys == null) {
81       secondaryKeys = new String JavaDoc[]{ };
82     }
83     allKeys = new String JavaDoc[secondaryKeys.length + 1];
84     allKeys[0] = primaryKey;
85     System.arraycopy(secondaryKeys, 0, allKeys, 1, secondaryKeys.length);
86   }
87   
88  /**
89   * get the primary key for this composite
90   */

91   public String JavaDoc getPrimaryKey() {
92     return primaryKey;
93   }
94   
95  /**
96   * get the array of secondary keys for this composite
97   */

98   public String JavaDoc[] getSecondaryKeys() {
99     return secondaryKeys;
100   }
101   
102  /**
103   * get an array of all keys in this composite. Note: the primary
104   * key is always the first member of this array
105   */

106   public String JavaDoc[] getAllKeys() {
107     return allKeys;
108   }
109
110  /**
111   * returns true if the specified object is the same as this composite
112   */

113   public boolean equals(Object JavaDoc obj) {
114     CompositeKey ck = (CompositeKey)obj;
115     int len = getAllKeys().length;
116     if (len != ck.getAllKeys().length) {
117       return false;
118     }
119     else {
120       for (int i = 0; i < len; i++) {
121         if (!StringUtils.isEqual(getAllKeys()[i], ck.getAllKeys()[i])) {
122           return false;
123         }
124       }
125       return true;
126     }
127   }
128   
129   public String JavaDoc toString() {
130     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
131     int len = getAllKeys().length;
132     int offlen = len - 1;
133     for (int i = 0; i < len; i++) {
134       sb.append(getAllKeys()[i]);
135       if (i < offlen) {
136         sb.append(COMMA);
137       }
138     }
139     return sb.toString();
140   }
141 }
Popular Tags