2e40da8b31535af4af6f649f5ca62c09348040df
[org.ibex.js.git] / src / org / ibex / js / JSString.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.js;
6
7 import org.ibex.util.*;
8 import java.util.*;
9
10 class JSString extends JSPrimitive {
11     final String s;
12     public JSString(String s) { this.s = s; }
13     public int hashCode() { return s.hashCode(); }
14     
15     public boolean jsequals(JS o) {
16         if(o == this) return true;
17         if(o instanceof JSString) {
18             return ((JSString)o).s.equals(s);
19         } else if(o instanceof JSNumber) {
20             return o.equals(this);
21         } else {
22             return false;
23         }
24     }
25     
26     private final static Map internHash = new HashMap();
27     static synchronized JS intern(String s) {
28         synchronized(internHash) {
29             JS js = (JS)internHash.get(s);
30             if(js == null) internHash.put(s,js = new Intern(s));
31             return js;
32         }
33     }
34     static class Intern extends JSString {
35         public Intern(String s) { super(s); }
36         protected void finalize() { synchronized(internHash) { internHash.put(s,null); } }
37     }
38     
39     public String coerceToString() { return s; }
40 }