/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.util; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import java.util.Collection; import java.util.Map; final class OffsetMapCache { private static final LoadingCache, Map> CACHE = CacheBuilder.newBuilder().weakValues().build(new CacheLoader, Map>() { @Override public Map load(final Collection key) { final Builder b = ImmutableMap.builder(); int i = 0; for (Object arg : key) { b.put(arg, i++); } return b.build(); } }); private OffsetMapCache() { throw new UnsupportedOperationException(); } @SuppressWarnings("unchecked") static Map offsetsFor(final Collection args) { return (Map) CACHE.getUnchecked(args); } }