licensing update to APSL 2.0
[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
9 class JSString extends JSPrimitive {
10     final String s;
11     public JSString(String s) { this.s = s; }
12     public int hashCode() { return s.hashCode(); }
13     
14     public boolean jsequals(JS o) {
15         if(o == this) return true;
16         if(o instanceof JSString) {
17             return ((JSString)o).s.equals(s);
18         } else if(o instanceof JSNumber) {
19             return o.jsequals(this);
20         } else {
21             return false;
22         }
23     }
24     
25     private final static Hash internHash = new Hash();
26     static synchronized JS intern(String s) {
27         synchronized(internHash) {
28             JS js = (JS)internHash.get(s);
29             if(js == null) internHash.put(s,js = new Intern(s));
30             return js;
31         }
32     }
33     static class Intern extends JSString {
34         public Intern(String s) { super(s); }
35         protected void finalize() { synchronized(internHash) { internHash.put(s,null); } }
36     }
37     
38     String coerceToString() { return s; }
39 }