001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.command;
018
019import java.io.Serializable;
020import java.lang.reflect.InvocationHandler;
021import java.lang.reflect.Method;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025
026public class ProxyInvocationHandler
027        implements Serializable, InvocationHandler {
028    
029    private Map<String, Object> map;
030    
031    /** Creates a new instance of ProxyInvocationHandler */
032    public ProxyInvocationHandler() {
033        map = new HashMap<>();
034    }
035    
036    @Override
037    public Object invoke(Object proxy, Method method, Object[] args) {
038        String name = method.getName();
039        int length = name.length();
040        
041        if(name.startsWith("get")) {
042            if(length == 3) {
043                if(args == null) {
044                    return Collections.unmodifiableMap(map);
045                } else {
046                    return map.get(args[0]);
047                }
048            } else {
049                return map.get(name.substring(3));
050            }
051        } else if(name.startsWith("is")) {
052            if(length == 2) {
053                return map.get(args[0]);
054            } else {
055                return map.get(name.substring(2));
056            }
057        } else if(name.startsWith("set")) {
058            if(length == 3) {
059                Object arg0 = args[0];
060
061                if(arg0 instanceof String) {
062                    return map.put((String)args[0], args[1]);
063                } else if(arg0 instanceof Map) {
064                    map.putAll((Map<String, Object>)arg0);
065                }
066            } else {
067                return map.put(name.substring(3), args[0]);
068            }
069        } else if(name.equals("reset")) {
070            map.clear();
071        } else if(name.equals("toString")) {
072            return new StringBuilder().append("{ map = ").append(map).append(" }").toString();
073        } else if(name.equals("hashCode")) {
074            return map.hashCode();
075        } else if(name.equals("equals")) {
076            return map.equals(args[0]);
077        }
078        
079        return null;
080    }
081    
082}