001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.graph.manager; 020 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.HashMap; 024 025/** 026 * Warning: this is a special map-like construct that suits only and should be used only in this package! 027 * It has the following properties: 028 * <ul> 029 * <li>memorizes once calculated hashCode</li> 030 * <li>once hashCode calculated, goes into "read only" mode (put method will fail)</li> 031 * <li>otherwise all the rest is same as for {@link HashMap}</li> 032 * </ul> 033 * 034 * This class is not a generic map class; only those methods are "protected" that are in use in this very 035 * package. 036 * 037 * @param <K> 038 * @param <V> 039 */ 040public class MMap<K, V> { 041 private static final MMap<?, ?> EMPTY_MAP = new MMap<>(new HashMap<>(0)).done(); 042 043 @SuppressWarnings("unchecked") 044 public static <K, V> MMap<K, V> empty() { 045 return (MMap<K, V>) MMap.EMPTY_MAP; 046 } 047 048 public static <K, V> MMap<K, V> copy(MMap<K, V> orig) { 049 return new MMap<>(new HashMap<>(orig.delegate)); 050 } 051 052 public static <K, V> MMap<K, Collection<V>> copyWithKey(K key, MMap<K, Collection<V>> orig) { 053 HashMap<K, Collection<V>> delegateLocal = new HashMap<>(orig.delegate); 054 delegateLocal.computeIfPresent(key, (k, v) -> new ArrayList<>(v)); 055 return new MMap<>(delegateLocal); 056 } 057 058 protected final HashMap<K, V> delegate; 059 060 private MMap(HashMap<K, V> delegate) { 061 this.delegate = delegate; 062 } 063 064 public boolean containsKey(K key) { 065 return delegate.containsKey(key); 066 } 067 068 public V get(K key) { 069 return delegate.get(key); 070 } 071 072 public V put(K key, V value) { 073 return delegate.put(key, value); 074 } 075 076 public MMap<K, V> done() { 077 return new DoneMMap<>(delegate); 078 } 079 080 @Override 081 public int hashCode() { 082 throw new IllegalStateException("MMap is not done yet"); 083 } 084 085 @Override 086 public boolean equals(Object o) { 087 throw new IllegalStateException("MMap is not done yet"); 088 } 089 090 private static class DoneMMap<K, V> extends MMap<K, V> { 091 private final int hashCode; 092 093 private DoneMMap(HashMap<K, V> delegate) { 094 super(delegate); 095 this.hashCode = delegate.hashCode(); 096 } 097 098 @Override 099 public V put(K key, V value) { 100 throw new IllegalStateException("Done MMap is immutable"); 101 } 102 103 @Override 104 public MMap<K, V> done() { 105 return this; 106 } 107 108 @Override 109 public int hashCode() { 110 return hashCode; 111 } 112 113 @Override 114 public boolean equals(Object o) { 115 if (!(o instanceof MMap)) { 116 return false; 117 } 118 MMap<?, ?> other = (MMap<?, ?>) o; 119 return delegate.equals(other.delegate); 120 } 121 } 122}