1 | package org.apollo.util;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * Creates default names for tracks.
|
---|
7 | *
|
---|
8 | * @author Brook Novak
|
---|
9 | *
|
---|
10 | */
|
---|
11 | public class TrackNameCreator {
|
---|
12 |
|
---|
13 | private static HashMap<String, Integer> nameCounters = new HashMap<String, Integer>();
|
---|
14 |
|
---|
15 | private TrackNameCreator() {}
|
---|
16 |
|
---|
17 |
|
---|
18 | private static final String DEFAULT_BASENAME = "untitled";
|
---|
19 |
|
---|
20 | // TODO: LOADING AND SAVING OF COUNTER MAP
|
---|
21 |
|
---|
22 | /**
|
---|
23 | *
|
---|
24 | * @param nameBase
|
---|
25 | * If null, the defulat name
|
---|
26 | * @return
|
---|
27 | */
|
---|
28 | public static String getDefaultName() {
|
---|
29 | return getNameCopy(null);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Copies a name - increments counter value at the end of the name.
|
---|
34 | *
|
---|
35 | * @param name
|
---|
36 | * If null then a default name copy is supplied. The name can already
|
---|
37 | * include the counter - this will be extracted if so.
|
---|
38 | *
|
---|
39 | * @return
|
---|
40 | * A copy of the given name. Never null.
|
---|
41 | */
|
---|
42 | public static String getNameCopy(String name) {
|
---|
43 |
|
---|
44 | if (name == null) name = DEFAULT_BASENAME;
|
---|
45 |
|
---|
46 | int i;
|
---|
47 | for (i = name.length() - 1; i > 0; i--) {
|
---|
48 | if (!Character.isDigit(name.charAt(i))) {
|
---|
49 | break;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | // Extract case sensitive basename
|
---|
54 | if (i <= 0) {
|
---|
55 | name = DEFAULT_BASENAME;
|
---|
56 | } else {
|
---|
57 | name = name.substring(0, i + 1);
|
---|
58 | }
|
---|
59 |
|
---|
60 | Integer count = nameCounters.get(name.toLowerCase());
|
---|
61 |
|
---|
62 | if (count == null) {
|
---|
63 | count = new Integer(1);
|
---|
64 | } else {
|
---|
65 | count = new Integer(count.intValue() + 1);
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Add or replace current count for this namebase
|
---|
69 | nameCounters.put(name.toLowerCase(), count);
|
---|
70 |
|
---|
71 | return name + count.toString();
|
---|
72 |
|
---|
73 | }
|
---|
74 | }
|
---|