001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019 package lucee.runtime.type;
020
021import lucee.runtime.exp.ExpressionException;
022import lucee.runtime.exp.PageException;
023import lucee.runtime.exp.PageRuntimeException;
024
025/**
026 * a read only Struct if flag is set to readonly 
027 */
028public class ReadOnlyStruct extends StructImpl {
029        
030
031        private boolean isReadOnly=false;
032        
033        /**
034         * sets if scope is readonly or not
035         * @param isReadOnly
036         */
037        public void setReadOnly(boolean isReadOnly) {
038                this.isReadOnly=isReadOnly;
039        }
040        
041        @Override
042        public Object remove(Collection.Key key) throws PageException {
043                if(isReadOnly)throw new ExpressionException("can't remove key ["+key.getString()+"] from struct, struct is readonly");
044                return super.remove (key);
045        }
046        
047        @Override
048        public Object removeEL(Collection.Key key) {
049                if(isReadOnly)return null;
050                return super.removeEL (key);
051        }
052        
053        public void removeAll() {
054                if(!isReadOnly)super.clear();
055        }
056
057        @Override
058        public Object set(Collection.Key key, Object value) throws PageException {
059                if(isReadOnly)throw new ExpressionException("can't set key ["+key.getString()+"] to struct, struct is readonly");
060                return super.set (key,value);
061        }
062        
063        @Override
064        public Object setEL(Collection.Key key, Object value) {
065                if(!isReadOnly)super.setEL (key,value);
066                return value;
067        }
068        
069
070        @Override
071        public Collection duplicate(boolean deepCopy) {
072                ReadOnlyStruct trg=new ReadOnlyStruct();
073                trg.isReadOnly=isReadOnly;
074                copy(this, trg, deepCopy);
075                return trg;
076        }
077        
078
079        @Override
080        public void clear() {
081                if(isReadOnly)throw new PageRuntimeException(new ExpressionException("can't clear struct, struct is readonly"));
082                super.clear();
083        }
084}