KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_Key


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_Key
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 /**
25
26     Key for these objects is an array of objects
27
28     value - Integer or String - implies what object should be used in the cache.
29     waitms - time to wait in ms on a set or create (simulates the object being loaded into the cache).
30     canFind - true of the object can be found on a set, false if it can't. (simulates a request for a non-existent object)
31     raiseException - true if an exception should be raised during set or create identity
32
33
34 */

35 public class T_Key {
36
37     private Object JavaDoc value;
38     private long waitms;
39     private boolean canFind;
40     private boolean raiseException;
41
42     public static T_Key simpleInt(int value) {
43         return new T_Key(new Integer JavaDoc(value), 0, true, false);
44     }
45     public static T_Key dontFindInt(int value) {
46         return new T_Key(new Integer JavaDoc(value), 0, false, false);
47     }
48     public static T_Key exceptionInt(int value) {
49         return new T_Key(new Integer JavaDoc(value), 0, true, true);
50     }
51     
52     /**
53         48%/48%/4% chance of Int/String/invalid key
54         90%/5%/5% chance of can find / can't find / raise exception
55     */

56     public static T_Key randomKey() {
57
58         double rand = Math.random();
59         T_Key tkey = new T_Key();
60
61         if (rand < 0.48)
62             tkey.value = new Integer JavaDoc((int) (100.0 * rand));
63         else if (rand < 0.96)
64             tkey.value = new Integer JavaDoc((int) (100.0 * rand));
65         else
66             tkey.value = Boolean.FALSE;
67
68         rand = Math.random();
69
70         if (rand < 0.90)
71             tkey.canFind = true;
72         else if (rand < 0.95)
73             tkey.canFind = false;
74         else {
75             tkey.canFind = true;
76             tkey.raiseException = false;
77         }
78
79         rand = Math.random();
80
81         if (rand < 0.30) {
82             tkey.waitms = (long) (rand * 1000.0); // Range 0 - 0.3 secs
83
}
84
85         return tkey;
86     }
87
88     private T_Key() {
89     }
90
91
92     private T_Key(Object JavaDoc value, long waitms, boolean canFind, boolean raiseException) {
93
94         this.value = value;
95         this.waitms = waitms;
96         this.canFind = canFind;
97         this.raiseException = raiseException;
98     }
99
100     public Object JavaDoc getValue() {
101         return value;
102     }
103
104     public long getWait() {
105         return waitms;
106     }
107
108     public boolean canFind() {
109         return canFind;
110     }
111
112     public boolean raiseException() {
113         return raiseException;
114     }
115
116     public boolean equals(Object JavaDoc other) {
117         if (other instanceof T_Key) {
118             return value.equals(((T_Key) other).value);
119         }
120         return false;
121     }
122
123     public int hashCode() {
124         return value.hashCode();
125     }
126
127     public String JavaDoc toString() {
128         return value + " " + waitms + " " + canFind + " " + raiseException;
129     }
130 }
131
132
Popular Tags