Move utilities from 'frontend' to 'util'
They're also used in 'serialization'
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.utils;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommonSuppliers {
|
||||
private CommonSuppliers() {}
|
||||
|
||||
private static final Supplier<?> ARRAY_LIST_SUPPLIER = new Supplier() {
|
||||
@Override
|
||||
public List get() {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
};
|
||||
|
||||
private static final Supplier<?> LINKED_HASH_SET_SUPPLIER = new Supplier() {
|
||||
@Override
|
||||
public Set get() {
|
||||
return Sets.newLinkedHashSet();
|
||||
}
|
||||
};
|
||||
|
||||
private static final Supplier<?> HASH_SET_SUPPLIER = new Supplier() {
|
||||
@Override
|
||||
public Set get() {
|
||||
return Sets.newHashSet();
|
||||
}
|
||||
};
|
||||
|
||||
public static <T> Supplier<List<T>> getArrayListSupplier() {
|
||||
//noinspection unchecked
|
||||
return (Supplier<List<T>>) ARRAY_LIST_SUPPLIER;
|
||||
}
|
||||
|
||||
public static <T> Supplier<Set<T>> getLinkedHashSetSupplier() {
|
||||
//noinspection unchecked
|
||||
return (Supplier<Set<T>>) LINKED_HASH_SET_SUPPLIER;
|
||||
}
|
||||
|
||||
public static <T> Supplier<Set<T>> getHashSetSupplier() {
|
||||
//noinspection unchecked
|
||||
return (Supplier<Set<T>>) HASH_SET_SUPPLIER;
|
||||
}
|
||||
|
||||
public static <K, V> SetMultimap<K, V> newLinkedHashSetHashSetMultimap() {
|
||||
return Multimaps.newSetMultimap(Maps.<K, Collection<V>>newHashMap(), CommonSuppliers.<V>getLinkedHashSetSupplier());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.utils;
|
||||
|
||||
public abstract class RecursionIntolerantLazyValue<T> {
|
||||
|
||||
private enum State {
|
||||
NOT_COMPUTED,
|
||||
BEING_COMPUTED,
|
||||
COMPUTED,
|
||||
ERROR
|
||||
}
|
||||
|
||||
private State state = State.NOT_COMPUTED;
|
||||
private T value;
|
||||
|
||||
protected abstract T compute();
|
||||
|
||||
protected T getValueOnErrorReentry() {
|
||||
throw new ReenteringLazyValueComputationException();
|
||||
}
|
||||
|
||||
public boolean isComputed() {
|
||||
return state == State.ERROR || state == State.COMPUTED;
|
||||
}
|
||||
|
||||
public final T get() {
|
||||
switch (state) {
|
||||
case NOT_COMPUTED:
|
||||
state = State.BEING_COMPUTED;
|
||||
value = compute();
|
||||
state = State.COMPUTED;
|
||||
return value;
|
||||
case BEING_COMPUTED:
|
||||
state = State.ERROR;
|
||||
throw new ReenteringLazyValueComputationException();
|
||||
case COMPUTED:
|
||||
return value;
|
||||
case ERROR:
|
||||
return getValueOnErrorReentry();
|
||||
}
|
||||
throw new IllegalStateException("Unreachable");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ReenteringLazyValueComputationException extends RuntimeException {
|
||||
public ReenteringLazyValueComputationException() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user