001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import java.util.Collections;
020import java.util.Map;
021
022import org.apache.camel.spi.EndpointUtilizationStatistics;
023import org.apache.camel.util.LRUCache;
024import org.apache.camel.util.LRUCacheFactory;
025
026public class DefaultEndpointUtilizationStatistics implements EndpointUtilizationStatistics {
027
028    private final LRUCache<String, Long> map;
029
030    @SuppressWarnings("unchecked")
031    public DefaultEndpointUtilizationStatistics(int maxCapacity) {
032        this.map = LRUCacheFactory.newLRUCache(16, maxCapacity, false);
033    }
034
035    @Override
036    public int maxCapacity() {
037        return map.getMaxCacheSize();
038    }
039
040    @Override
041    public int size() {
042        return map.size();
043    }
044
045    @Override
046    public void onHit(String uri) {
047        map.compute(uri, (key, current) -> {
048            if (current == null) {
049                return 1L;
050            } else {
051                return ++current;
052            }
053        });
054    }
055
056    @Override
057    public void remove(String uri) {
058        map.remove(uri);
059    }
060
061    @Override
062    public Map<String, Long> getStatistics() {
063        return Collections.unmodifiableMap(map);
064    }
065
066    @Override
067    public void clear() {
068        map.clear();
069    }
070}