2 * Copyright 2000-2011 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.ether.dependencyView;
18 import com.intellij.util.io.DataExternalizer;
19 import com.intellij.util.io.KeyDescriptor;
20 import com.intellij.util.io.PersistentHashMap;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.LinkedList;
29 * Created by IntelliJ IDEA.
33 * To change this template use File | Settings | File Templates.
35 public class PersistentMaplet<K, V> implements Maplet<K, V> {
36 private final PersistentHashMap<K, V> myMap;
38 public PersistentMaplet(final File file, final KeyDescriptor<K> k, final DataExternalizer<V> v) {
40 myMap = new PersistentHashMap<K, V>(file, k, v);
42 catch (IOException e) {
43 throw new RuntimeException(e);
48 public boolean containsKey(final Object key) {
50 return myMap.containsMapping((K)key);
52 catch (IOException e) {
53 throw new RuntimeException(e);
58 public V get(final Object key) {
60 return myMap.get((K)key);
62 catch (IOException e) {
63 throw new RuntimeException(e);
68 public void put(final K key, final V value) {
70 myMap.put(key, value);
72 catch (IOException e) {
73 throw new RuntimeException(e);
78 public void putAll(final Maplet<K, V> m) {
80 for (Map.Entry<K, V> e : m.entrySet()) {
81 myMap.put(e.getKey(), e.getValue());
84 catch (IOException e) {
85 throw new RuntimeException(e);
90 public void remove(final Object key) {
94 catch (IOException e) {
95 throw new RuntimeException(e);
100 public void close() {
104 catch (IOException e) {
105 throw new RuntimeException(e);
109 public void flush(boolean memoryCachesOnly) {
110 if (memoryCachesOnly) {
111 if (myMap.isDirty()) {
112 myMap.dropMemoryCaches();
121 public Collection<K> keyCollection() {
123 return myMap.getAllKeysWithExistingMapping();
125 catch (IOException e) {
126 throw new RuntimeException(e);
131 public Collection<Map.Entry<K, V>> entrySet() {
132 final Collection<Map.Entry<K, V>> result = new LinkedList<Map.Entry<K, V>>();
135 for (final K key : myMap.getAllKeysWithExistingMapping()) {
136 final V value = myMap.get(key);
138 final Map.Entry<K, V> entry = new Map.Entry<K, V>() {
145 public V getValue() {
150 public V setValue(V value) {
160 catch (IOException e) {
161 throw new RuntimeException(e);