1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panels.apache.client.swing; |
|
27 |
|
28 import java.util.*; |
|
29 import com.oracle.solaris.scf.common.ScfException; |
|
30 import com.oracle.solaris.vp.panel.common.api.smf_old.*; |
|
31 import com.oracle.solaris.vp.panel.common.smf.ServiceBean; |
|
32 |
|
33 public class PropertyGroupNamePool { |
|
34 // |
|
35 // Static data |
|
36 // |
|
37 |
|
38 private static final int MINIMUM = 1; |
|
39 |
|
40 // |
|
41 // Instance data |
|
42 // |
|
43 |
|
44 private ServiceBean service; |
|
45 private String prefix; |
|
46 private List<Integer> existing = new LinkedList<Integer>(); |
|
47 private List<Integer> reserved = new LinkedList<Integer>(); |
|
48 private int next; |
|
49 |
|
50 // |
|
51 // Constructors |
|
52 // |
|
53 |
|
54 public PropertyGroupNamePool(ServiceBean service, String prefix) |
|
55 throws ScfException { |
|
56 |
|
57 this.service = service; |
|
58 this.prefix = prefix; |
|
59 |
|
60 refresh(); |
|
61 } |
|
62 |
|
63 // |
|
64 // PropertyGroupNamePool methods |
|
65 // |
|
66 |
|
67 public String get() { |
|
68 String name = prefix + next; |
|
69 |
|
70 int index = Collections.binarySearch(reserved, next); |
|
71 assert index < 0; |
|
72 index = -1 - index; |
|
73 reserved.add(index, next); |
|
74 |
|
75 next = getNext(next); |
|
76 |
|
77 return name; |
|
78 } |
|
79 |
|
80 @SuppressWarnings({"unchecked"}) |
|
81 public boolean put(int ord) { |
|
82 boolean found = false; |
|
83 List[] lists = { |
|
84 reserved, existing |
|
85 }; |
|
86 |
|
87 for (List<Integer> list : lists) { |
|
88 int index = Collections.binarySearch(list, ord); |
|
89 found = index >= 0; |
|
90 if (found) { |
|
91 list.remove(index); |
|
92 if (ord < next) { |
|
93 next = ord; |
|
94 } |
|
95 break; |
|
96 } |
|
97 } |
|
98 |
|
99 return found; |
|
100 } |
|
101 |
|
102 public boolean put(String name) { |
|
103 try { |
|
104 return put(toOrd(name)); |
|
105 } catch (NumberFormatException e) { |
|
106 return false; |
|
107 } |
|
108 } |
|
109 |
|
110 public void refresh() throws ScfException { |
|
111 existing.clear(); |
|
112 for (PropertyGroup pg : service.getPropertyGroups()) { |
|
113 String name = pg.getName(); |
|
114 try { |
|
115 int ord = toOrd(name); |
|
116 int index = Collections.binarySearch(existing, ord); |
|
117 if (index < 0) { |
|
118 index = -1 - index; |
|
119 existing.add(index, ord); |
|
120 } |
|
121 index = Collections.binarySearch(reserved, ord); |
|
122 if (index >= 0) { |
|
123 reserved.remove(index); |
|
124 } |
|
125 } catch (NumberFormatException skip) { |
|
126 } |
|
127 } |
|
128 next = getNext(MINIMUM); |
|
129 } |
|
130 |
|
131 // |
|
132 // Private methods |
|
133 // |
|
134 |
|
135 private int getNext(int from) { |
|
136 while (Collections.binarySearch(existing, from) >= 0 || |
|
137 Collections.binarySearch(reserved, from) >= 0) { |
|
138 from++; |
|
139 } |
|
140 return from; |
|
141 } |
|
142 |
|
143 private int toOrd(String name) { |
|
144 if (name.startsWith(prefix)) { |
|
145 return Integer.parseInt(name.substring(prefix.length())); |
|
146 } |
|
147 throw new NumberFormatException(); |
|
148 } |
|
149 } |
|