KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > CircularIdentityList


1 /*
2  * @(#)CircularIdentityList.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.gtk;
8
9 import java.util.*;
10
11 /**
12  * An circular linked list like data structure that uses identity for equality
13  * testing.
14  *
15  * @version 1.5, 12/19/03
16  * @author Scott Violet
17  */

18 class CircularIdentityList implements Cloneable JavaDoc {
19     private Property property;
20
21     /**
22      * Sets a particular value.
23      */

24     public synchronized void set(Object JavaDoc key, Object JavaDoc value) {
25         if (property == null) {
26             property = new Property(key, value, null);
27         }
28         else {
29             Property p = property;
30             Property last = p;
31
32             do {
33                 if (p.key == key) {
34                     p.value = value;
35                     property = p;
36                     return;
37                 }
38                 last = p;
39                 p = p.next;
40             } while (p != property && p != null);
41             // not defined
42
if (value != null) {
43                 if (p == null) {
44                     // Only one element
45
p = property;
46                 }
47                 property = new Property(key, value, p);
48                 last.next = property;
49             }
50         }
51     }
52
53     /**
54      * Returns the value currently being referenced.
55      */

56     public synchronized Object JavaDoc get() {
57         if (property == null) {
58             return null;
59         }
60         return property.value;
61     }
62
63     /**
64      * Returns the value for a specific key.
65      */

66     public synchronized Object JavaDoc get(Object JavaDoc key) {
67         if (property == null) {
68             return null;
69         }
70         Property p = property;
71
72         do {
73             if (p.key == key) {
74                 return p.value;
75             }
76             p = p.next;
77         } while (p != property && p != null);
78         return null;
79     }
80
81     /**
82      * Advanced the list returning the next key. This will only return
83      * null if the list is empty.
84      */

85     public synchronized Object JavaDoc next() {
86         if (property == null) {
87             return null;
88         }
89         if (property.next == null) {
90             return property.key;
91         }
92         property = property.next;
93         return property.key;
94     }
95
96     public synchronized Object JavaDoc clone() {
97         try {
98             CircularIdentityList list = (CircularIdentityList)super.clone();
99
100             if (property != null) {
101                 list.property = (Property)property.clone();
102
103                 Property last = list.property;
104
105                 while (last.next != null && last.next != property) {
106                     last.next = (Property)last.next.clone();
107                     last = last.next;
108                 }
109                 last.next = list.property;
110             }
111             return list;
112         } catch (CloneNotSupportedException JavaDoc cnse) {
113         }
114         return null;
115     }
116
117
118     static class Property implements Cloneable JavaDoc {
119         Object JavaDoc key;
120         Object JavaDoc value;
121         Property next;
122
123         Property(Object JavaDoc key, Object JavaDoc value, Property next) {
124             this.key = key;
125             this.value = value;
126             this.next = next;
127         }
128
129         public Object JavaDoc clone() {
130             try {
131                 return super.clone();
132             } catch (CloneNotSupportedException JavaDoc cnse) {
133             }
134             return null;
135         }
136     }
137 }
138
Popular Tags