001    /*
002     * Copyright (C) 2011 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     *
007     * published by the Free Software Foundation; either version 2.1 of
008     * the License, or (at your option) any later version.
009     *
010     * This software is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this software; if not, write to the Free
017     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
019     */
020    
021    package org.crsh.command.impl;
022    
023    import org.crsh.command.InvocationContext;
024    
025    import java.util.*;
026    
027    /**
028     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
029     */
030    public class AttributesMap extends AbstractMap<String, Object> {
031    
032      /** . */
033      private InvocationContext<?, ?> context;
034    
035      /** . */
036      private Map<String, Object> delegate;
037    
038      /** . */
039      private Set<Entry<String, Object>> entries;
040    
041      public AttributesMap(InvocationContext<?, ?> context, Map<String, Object> delegate) {
042        this.context = context;
043        this.delegate = delegate;
044      }
045    
046      private Entry<String, Object> out = new Entry<String, Object>() {
047        public String getKey() {
048          return "out";
049        }
050        public Object getValue() {
051          return context.getWriter();
052        }
053        public Object setValue(Object value) {
054          throw new IllegalArgumentException("Cannot change out entry");
055        }
056      };
057    
058      @Override
059      public Object get(Object key) {
060        if ("out".equals(key)) {
061          return out.getValue();
062        } else {
063          return delegate.get(key);
064        }
065      }
066    
067      @Override
068      public Object put(String key, Object value) {
069        if ("out".equals(key)) {
070          throw new IllegalArgumentException("Cannot change out entry");
071        } else {
072          return delegate.put(key, value);
073        }
074      }
075    
076      @Override
077      public boolean containsKey(Object key) {
078        if ("out".equals(key)) {
079          return true;
080        } else {
081          return delegate.containsKey(key);
082        }
083      }
084    
085      @Override
086      public Object remove(Object key) {
087        if ("out".equals(key)) {
088          throw new IllegalArgumentException("Cannot change out entry");
089        } else {
090          return delegate.remove(key);
091        }
092      }
093    
094      @Override
095      public void clear() {
096        throw new UnsupportedOperationException("Cannot clear the attribute map");
097      }
098    
099      @Override
100      public Set<Entry<String, Object>> entrySet() {
101        if (entries == null) {
102          entries = new AbstractSet<Entry<String, Object>>() {
103            @Override
104            public Iterator<Entry<String, Object>> iterator() {
105              return new Iterator<Entry<String, Object>>() {
106                Iterator<Entry<String, Object>> i = delegate.entrySet().iterator();
107                Entry<String, Object> next = out;
108                public boolean hasNext() {
109                  while (next == null && i.hasNext()) {
110                    Entry<String, Object> tmp = i.next();
111                    if (!next.getKey().equals("out")) {
112                      next = tmp;
113                    }
114                  }
115                  return next != null;
116                }
117                public Entry<String, Object> next() {
118                  if (hasNext()) {
119                    Entry<String, Object> tmp = next;
120                    next = null;
121                    return tmp;
122                  } else {
123                    throw new NoSuchElementException();
124                  }
125                }
126                public void remove() {
127                  throw new UnsupportedOperationException();
128                }
129              };
130            }
131            @Override
132            public int size() {
133              return delegate.size() + (delegate.containsKey("out") ? 0 : 1);
134            }
135          };
136        }
137        return entries;
138      }
139    }