用coffeescript实现类java的Map类

时间:2021-12-22 01:53:45
class Map
    constructor : -> 
        @entry = {}
        @count = 0
    
    size : -> 
        return @count
    
    isEmpty : -> 
        return @count == 0
    
    containsKey : (key) ->
        if @isEmpty()
            return false
        return @entry.hasOwnProperty key
    
    containsValue : (val)->
        if @isEmpty()
            return false
        for key,_val of @entry
            if _val == val
                return true
        return false
    
    get : (key)->
        if @isEmpty()
            return null
        if @containsKey key
            return @entry[key]     
        return null
    
    put : (key, val)->
        if !@entry.hasOwnProperty key
            @count += 1;  
        @entry[key] = val
        return @
    
    remove : (key)-> 
        if @isEmpty()
            return false
        if @containsKey key
            delete @entry[key]
            @count -= 1
            return true
        return false    
    
    putAll : (map)->  
        if !map instanceof Map
            return false
        entry = map.entry
        for key,val of entry
            @put(key, val)
        return true
    
    clear : ->
        @entry = {}
        @count = 0
        return @ 
    
    values : ->  
        vals = []
        for key,val of @entry
            vals.push val
        return vals
        
    keySet : ->  
        keys = []
        for key,val of @entry
            keys.push key
        return keys
    
    entrySet : ->  
        return @entry
    
    toString : ->  
        if typeof JSON == "undefined"
            throw new Error "JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+)."
        return JSON.stringify @entry
    
    valueOf : ->  
        return @toString()

 使用coffeescript编译后生成代码:

(function() {
  var Map;
  var __hasProp = Object.prototype.hasOwnProperty;
  Map = function() {
    this.entry = {};
    this.count = 0;
    return this;
  };
  Map.prototype.size = function() {
    return this.count;
  };
  Map.prototype.isEmpty = function() {
    return this.count === 0;
  };
  Map.prototype.containsKey = function(key) {
    if (this.isEmpty()) {
      return false;
    }
    return this.entry.hasOwnProperty(key);
  };
  Map.prototype.containsValue = function(val) {
    var _a, _val, key;
    if (this.isEmpty()) {
      return false;
    }
    _a = this.entry;
    for (key in _a) {
      if (!__hasProp.call(_a, key)) continue;
      _val = _a[key];
      if (_val === val) {
        return true;
      }
    }
    return false;
  };
  Map.prototype.get = function(key) {
    if (this.isEmpty()) {
      return null;
    }
    if (this.containsKey(key)) {
      return this.entry[key];
    }
    return null;
  };
  Map.prototype.put = function(key, val) {
    if (!this.entry.hasOwnProperty(key)) {
      this.count += 1;
    };
    this.entry[key] = val;
    return this;
  };
  Map.prototype.remove = function(key) {
    if (this.isEmpty()) {
      return false;
    }
    if (this.containsKey(key)) {
      delete this.entry[key];
      this.count -= 1;
      return true;
    }
    return false;
  };
  Map.prototype.putAll = function(map) {
    var _a, entry, key, val;
    if (!map instanceof Map) {
      return false;
    }
    entry = map.entry;
    _a = entry;
    for (key in _a) {
      if (!__hasProp.call(_a, key)) continue;
      val = _a[key];
      this.put(key, val);
    }
    return true;
  };
  Map.prototype.clear = function() {
    this.entry = {};
    this.count = 0;
    return this;
  };
  Map.prototype.values = function() {
    var _a, key, val, vals;
    vals = [];
    _a = this.entry;
    for (key in _a) {
      if (!__hasProp.call(_a, key)) continue;
      val = _a[key];
      vals.push(val);
    }
    return vals;
  };
  Map.prototype.keySet = function() {
    var _a, key, keys, val;
    keys = [];
    _a = this.entry;
    for (key in _a) {
      if (!__hasProp.call(_a, key)) continue;
      val = _a[key];
      keys.push(key);
    }
    return keys;
  };
  Map.prototype.entrySet = function() {
    return this.entry;
  };
  Map.prototype.toString = function() {
    if (typeof JSON === "undefined") {
      throw new Error("JSON object is not supported. Please check your browser version (IE8+, Firefox11+, Chrome19+, Safari5.1+).");
    }
    return JSON.stringify(this.entry);
  };
  Map.prototype.valueOf = function() {
    return this.toString();
  };
})();