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 **/
019package lucee.runtime.type.scope;
020
021
022
023/**
024 * creates Local and Argument scopes and recyle it
025 */
026public final class ScopeFactory {
027    
028    int argumentCounter=0;
029    Argument[] arguments=new Argument[]{
030            new ArgumentImpl(),
031            new ArgumentImpl(),
032            new ArgumentImpl(),
033            new ArgumentImpl(),
034            new ArgumentImpl(),
035            new ArgumentImpl(),
036            new ArgumentImpl(),
037            new ArgumentImpl(),
038            new ArgumentImpl(),
039            new ArgumentImpl(),
040            new ArgumentImpl(),
041            new ArgumentImpl(),
042            new ArgumentImpl(),
043            new ArgumentImpl(),
044            new ArgumentImpl(),
045            new ArgumentImpl(),
046            new ArgumentImpl(),
047            new ArgumentImpl(),
048            new ArgumentImpl()
049    };
050    
051    int localCounter=0;
052    LocalImpl[] locals=new LocalImpl[]{
053            new LocalImpl(),
054            new LocalImpl(),
055            new LocalImpl(),
056            new LocalImpl(),
057            new LocalImpl(),
058            new LocalImpl(),
059            new LocalImpl(),
060            new LocalImpl(),
061            new LocalImpl(),
062            new LocalImpl(),
063            new LocalImpl(),
064            new LocalImpl(),
065            new LocalImpl(),
066            new LocalImpl(),
067            new LocalImpl(),
068            new LocalImpl(),
069            new LocalImpl(),
070            new LocalImpl(),
071            new LocalImpl(),
072            new LocalImpl(),
073            new LocalImpl(),
074            new LocalImpl(),
075            new LocalImpl(),
076            new LocalImpl(),
077            new LocalImpl(),
078            new LocalImpl(),
079            new LocalImpl()
080    };
081        private static int count=0;
082    
083    /**
084     * @return returns a Argument scope
085     */
086    public Argument getArgumentInstance() {
087        if(argumentCounter<arguments.length) {
088                return arguments[argumentCounter++];
089        }
090        return new ArgumentImpl();
091    }
092
093    /**
094     * @return retruns a Local Instance
095     */
096    public LocalImpl getLocalInstance() {
097        if(localCounter<locals.length) {
098            return locals[localCounter++];
099        }
100        return new LocalImpl();
101    }
102    
103    /** 
104     * @param argument  recycle a Argument scope for reuse
105     */
106    public void recycle(Argument argument) {
107        if(argumentCounter<=0 || argument.isBind()) return;
108        argument.release();
109        arguments[--argumentCounter]=argument;
110    }
111
112    /**
113     * @param local recycle a Local scope for reuse
114     */
115    public void recycle(LocalImpl local) {
116        if(localCounter<=0  || local.isBind()) return;
117        local.release();
118        locals[--localCounter]=local;
119    }
120    
121    /**
122     * cast a int scope definition to a string definition
123     * @param scope
124     * @return
125     */
126    public static String toStringScope(int scope, String defaultValue) {
127        switch(scope) {
128        case Scope.SCOPE_APPLICATION:   return "application";
129        case Scope.SCOPE_ARGUMENTS:     return "arguments";
130        case Scope.SCOPE_CALLER:        return "caller";
131        case Scope.SCOPE_CGI:           return "cgi";
132        case Scope.SCOPE_CLIENT:        return "client";
133        case Scope.SCOPE_COOKIE:        return "cookie";
134        case Scope.SCOPE_FORM:          return "form";
135        case ScopeSupport.SCOPE_VAR:         
136        case Scope.SCOPE_LOCAL:         return "local";
137        case Scope.SCOPE_REQUEST:       return "request";
138        case Scope.SCOPE_SERVER:        return "server";
139        case Scope.SCOPE_SESSION:       return "session";
140        case Scope.SCOPE_UNDEFINED:     return "undefined";
141        case Scope.SCOPE_URL:           return "url";
142        case Scope.SCOPE_VARIABLES:     return "variables";
143        case ScopeSupport.SCOPE_CLUSTER:     return "cluster";
144        }
145        
146        
147        
148        return defaultValue;
149    }
150    
151    /* *
152     * return a string list of all scope names
153     * @param orderAsInvoked when true the order of the list is the same as they are invoked by the Undefined Scope, when the value is false the list is returned in a alphabetic order 
154     * @return
155     * /
156    public static String[] getAllScopes(boolean orderAsInvoked) {
157        if(!orderAsInvoked){
158                return new String[]{
159                                "application","arguments","caller","cgi","client","cluster","cookie","form","local","request","server","session","url","variables"
160            };
161        }
162        return new String[]{
163                        "local","arguments","variables","cgi","url","form","cookie","client","application","caller","cluster","request","server","session"
164        };
165    }*/
166
167}