1
package org.jnode.vm;
2
3
import org.jnode.util.BootableHashMap;
4
5
/**
6
 *
7
 */
8
public class InternString extends VmSystemObject {
9
    private static BootableHashMap<String, String> bootInternTable;
10
    private static boolean booted = false;
11
12
    public static String internString(String instance) {
13
        if (booted) {
14
            return instance.intern();
15
        } else {
16
            if (Vm.getVm().isBootstrap()) {
17
                if (bootInternTable == null) {
18
                    bootInternTable = new BootableHashMap<String, String>();
19
                }
20
21
                instance = instance.intern();
22
                //todo the string interned after emiting bootInternTable will be lost 
23
                if (!bootInternTable.isLocked()) {
24
                    synchronized (bootInternTable) {
25
                        final String str = bootInternTable.get(instance);
26
                        if (str != null) {
27
                            return str;
28
                        }
29
                        bootInternTable.put(instance, instance);
30
                    }
31
                }
32
                return instance;
33
            } else {
34
                return instance.intern();
35
            }
36
        }
37
    }
38
39
    //todo protect it
40
    public static void boot() {
41
        booted = true;
42
    }
43
44
    //todo protect it
45
    public static BootableHashMap<String, String> getBootInternTable() {
46
        return bootInternTable;
47
    }
48
}