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.model.control.selector.server.evaluator;
018
019import com.echothree.model.data.selector.server.entity.Selector;
020import com.echothree.model.data.selector.server.entity.SelectorType;
021import java.util.HashMap;
022import java.util.Map;
023
024public class SelectorCache {
025    
026    SelectorType selectorType;
027    
028    Map<Selector, CachedSelector> cachedSelectors = new HashMap<>();
029    
030    /** Creates a new instance of SelectorCache */
031    public SelectorCache(SelectorType selectorType) {
032        super();
033
034        this.selectorType = selectorType;
035    }
036    
037    public CachedSelector getSelector(Selector selector) {
038        CachedSelector cachedSelector;
039        
040        if(selector.getLastDetail().getSelectorType().equals(selectorType)) {
041            cachedSelector = cachedSelectors.get(selector);
042            
043            if(cachedSelector == null) {
044                cachedSelector = new CachedSelector(selector);
045                cachedSelectors.put(selector, cachedSelector);
046            }
047        } else {
048            throw new IllegalArgumentException("incorrect selectorType");
049        }
050        
051        return cachedSelector;
052    }
053    
054}