1 | package desmoj.core.simulator; |
2 | |
3 | import java.util.HashMap; |
4 | |
5 | /** |
6 | * Keeps track of the names given for Schedulables within an experiment. To help |
7 | * identify individual entities, events and all other types of Schedulables, |
8 | * this class registers all names given to these objects. If an object is |
9 | * created with the same name as some other object before, a number is added to |
10 | * the object's name as a suffix. The number represents the amount of objects |
11 | * already created with that name. |
12 | * |
13 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
14 | * @author Tim Lechler |
15 | * |
16 | * Licensed under the Apache License, Version 2.0 (the "License"); |
17 | * you may not use this file except in compliance with the License. You |
18 | * may obtain a copy of the License at |
19 | * http://www.apache.org/licenses/LICENSE-2.0 |
20 | * |
21 | * Unless required by applicable law or agreed to in writing, software |
22 | * distributed under the License is distributed on an "AS IS" |
23 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
24 | * or implied. See the License for the specific language governing |
25 | * permissions and limitations under the License. |
26 | * |
27 | */ |
28 | public class NameCatalog { |
29 | |
30 | /** |
31 | * Stores the names of all Schedulables within an experiment. The names are |
32 | * used as the indexing value via the string's method to produce a hashing |
33 | * key value. The numbers for each individual name are stored as objects of |
34 | * their wrapper class. |
35 | */ |
36 | private HashMap<String,Integer> _catalog; |
37 | |
38 | /** |
39 | * Constructs a single namecatalog for registering names of all Schedulables |
40 | * within an experiment. |
41 | */ |
42 | NameCatalog() { |
43 | |
44 | super(); |
45 | _catalog = new HashMap<String,Integer>(); |
46 | |
47 | } |
48 | |
49 | /** |
50 | * Registers the given name in the namecatalog and returns the correct name |
51 | * with the added number if necessary. If a <code>null</code> reference is |
52 | * given as parameter, the returned name will be set to "unnamed" with the |
53 | * number of unnamed objects so far added as a suffix. |
54 | * |
55 | * @return java.lang.String : The registered name including the number if |
56 | * necessary |
57 | * @param name |
58 | * java.lang.String : The name for a new Schedulable |
59 | */ |
60 | String registeredName(String name) { |
61 | |
62 | if (name == null) |
63 | name = "unnamed"; |
64 | |
65 | Integer number = _catalog.get(name); |
66 | |
67 | if (number != null) { |
68 | int i = number.intValue(); |
69 | i++; |
70 | _catalog.put(name, i); |
71 | return name + "#" + i; |
72 | } else { |
73 | _catalog.put(name, 1); |
74 | return name + "#1"; |
75 | } |
76 | |
77 | } |
78 | |
79 | /** |
80 | * Returns the first part of name without added number as suffix. |
81 | * |
82 | * @return java.lang.String : The first part of the name without number suffix |
83 | * @param name |
84 | * java.lang.String : The name obtain the first part from. |
85 | */ |
86 | String getNameWithoutSuffix(String name) { |
87 | if (name == null || name.equals("unnamed") || name.indexOf("#") == -1) |
88 | return name; // no suffix to remove |
89 | else |
90 | return name.substring(0, name.lastIndexOf("#")); |
91 | } |
92 | } |