001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
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 */
016package org.talend.sdk.component.server.dao;
017
018import java.util.Map;
019import java.util.Objects;
020import java.util.concurrent.ConcurrentHashMap;
021
022import javax.enterprise.context.ApplicationScoped;
023
024import org.talend.sdk.component.runtime.manager.ServiceMeta;
025
026@ApplicationScoped
027public class ComponentActionDao {
028
029    private Map<ActionKey, ServiceMeta.ActionMeta> data = new ConcurrentHashMap<>();
030
031    public ActionKey createOrUpdate(final ServiceMeta.ActionMeta meta) {
032        final ActionKey key = new ActionKey(meta.getFamily(), meta.getType(), meta.getAction());
033        data.put(key, meta);
034        return key;
035    }
036
037    public ServiceMeta.ActionMeta findBy(final String component, final String type, final String action) {
038        return data.get(new ActionKey(component, type, action));
039    }
040
041    public void removeById(final ActionKey key) {
042        data.remove(key);
043    }
044
045    public static class ActionKey {
046
047        private final String component;
048
049        private final String type;
050
051        private final String name;
052
053        private final int hash;
054
055        private ActionKey(final String component, final String type, final String name) {
056            this.component = component;
057            this.name = name;
058            this.type = type;
059            this.hash = Objects.hash(component, type, name);
060        }
061
062        @Override
063        public boolean equals(final Object o) {
064            if (this == o) {
065                return true;
066            }
067            if (o == null || getClass() != o.getClass()) {
068                return false;
069            }
070            final ActionKey other = ActionKey.class.cast(o);
071            return Objects.equals(component, other.component) && Objects.equals(type, other.type)
072                    && Objects.equals(name, other.name);
073        }
074
075        @Override
076        public int hashCode() {
077            return hash;
078        }
079
080    }
081
082}