zoukankan      html  css  js  c++  java
  • 用coffeescript实现类java的Map类

    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();
      };
    })();
  • 相关阅读:
    linux查看用户组所有成员
    navicat for mysql 在Mac上安装后没有连接列表,就是左边的那一列连接项目怎么办?
    mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
    phpcms多站点表单统一到主站点管理的解决方案
    thinkphp5.0 session驱动方式问题汇总
    Python__开启进程的两种方式
    Python并发编程之操作系统理论部分
    操作系统简介
    Python__基于udp的套接字
    网络编程1
  • 原文地址:https://www.cnblogs.com/zfc2201/p/3500236.html
Copyright © 2011-2022 走看看