1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.script.util;
  27 import java.util.*;
  28 
  29 /**
  30  * Entry set implementation for Bindings implementations
  31  *
  32  * @author Mike Grogan
  33  * @since 1.6
  34  */
  35 public class BindingsEntrySet extends AbstractSet<Map.Entry<String, Object>> {
  36 
  37     private BindingsBase base;
  38     private String[] keys;
  39 
  40     public BindingsEntrySet(BindingsBase base) {
  41         this.base = base;
  42         keys = base.getNames();
  43     }
  44 
  45     public int size() {
  46         return keys.length;
  47     }
  48 
  49     public Iterator<Map.Entry<String, Object>> iterator() {
  50         return new BindingsIterator();
  51     }
  52 
  53     public class BindingsEntry implements Map.Entry<String, Object> {
  54         private String key;
  55         public BindingsEntry(String key) {
  56             this.key = key;
  57         }
  58 
  59         public Object setValue(Object value) {
  60             throw new UnsupportedOperationException();
  61         }
  62 
  63         public String getKey() {
  64             return key;
  65         }
  66 
  67         public Object getValue() {
  68             return base.get(key);
  69         }
  70 
  71     }
  72 
  73     public class BindingsIterator implements Iterator<Map.Entry<String, Object>> {
  74 
  75         private int current = 0;
  76         private boolean stale = false;
  77 
  78         public boolean hasNext() {
  79             return (current < keys.length);
  80         }
  81 
  82         public BindingsEntry next() {
  83             stale = false;
  84             return new BindingsEntry(keys[current++]);
  85         }
  86 
  87         public void remove() {
  88             if (stale || current == 0) {
  89                 throw new IllegalStateException();
  90             }
  91 
  92             stale = true;
  93             base.remove(keys[current - 1]);
  94         }
  95 
  96     }
  97 
  98 }