convert DataKey to kotlin

GitOrigin-RevId: 4532ca2961456b6ce74264cc5544cd0b83d84500
This commit is contained in:
Gregory.Shrago
2024-06-07 19:28:33 +03:00
committed by intellij-monorepo-bot
parent bb0ea4195e
commit 3bae155910
5 changed files with 81 additions and 97 deletions

View File

@@ -1,75 +1,57 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.actionSystem
package com.intellij.openapi.actionSystem;
import com.intellij.openapi.util.ValueKey;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.intellij.openapi.util.ValueKey
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.NonNls
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
/**
* Type-safe named key.
* <p/>
* Mainly used via {@link AnActionEvent#getData(DataKey)} calls and {@link DataProvider#getData(String)} implementations.
* <p/>
* Corresponding data for given {@code name} is provided by {@link DataProvider} implementations.
* Globally available data can be provided via {@link com.intellij.ide.impl.dataRules.GetDataRule} extension point.
*
*
* Mainly used via [AnActionEvent.getData] calls and [DataProvider.getData] implementations.
*
*
* Corresponding data for given `name` is provided by [DataProvider] implementations.
* Globally available data can be provided via [com.intellij.ide.impl.dataRules.GetDataRule] extension point.
*
* @param <T> Data type.
* @see CommonDataKeys
*
* @see com.intellij.openapi.actionSystem.PlatformDataKeys
*
* @see LangDataKeys
*/
public final class DataKey<T> implements ValueKey<T> {
private static final ConcurrentMap<String, DataKey<?>> ourDataKeyIndex = new ConcurrentHashMap<>();
private final String myName;
private DataKey(@NotNull String name) {
myName = name;
}
public static @NotNull <T> DataKey<T> create(@NotNull @NonNls String name) {
//noinspection unchecked
return (DataKey<T>)ourDataKeyIndex.computeIfAbsent(name, DataKey::new);
}
@ApiStatus.Internal
public static DataKey<?> @NotNull [] allKeys() {
return ourDataKeyIndex.values().toArray(new DataKey[0]);
}
@ApiStatus.Internal
public static int allKeysCount() {
return ourDataKeyIndex.size();
}
@Override
public @NotNull @NonNls String getName() {
return myName;
}
@Suppress("UNCHECKED_CAST")
class DataKey<out T> private constructor(override val name: String) : ValueKey<T> {
/**
* For short notation, use {@code MY_KEY.is(dataId)} instead of {@code MY_KEY.getName().equals(dataId)}.
* For short notation, use `MY_KEY.is(dataId)` instead of `MY_KEY.getName().equals(dataId)`.
*
* @param dataId key name
* @return {@code true} if name of DataKey equals to {@code dataId}, {@code false} otherwise
* @return `true` if name of DataKey equals to `dataId`, `false` otherwise
*/
public boolean is(String dataId) {
return myName.equals(dataId);
}
fun `is`(dataId: String?): Boolean = name == dataId
public @Nullable T getData(@NotNull DataContext dataContext) {
//noinspection unchecked
return (T)dataContext.getData(myName);
}
fun getData(dataContext: DataContext): T? = dataContext.getData(name) as T?
public @Nullable T getData(@NotNull DataProvider dataProvider) {
//noinspection unchecked
return (T)dataProvider.getData(myName);
fun getData(dataProvider: DataProvider): T? = dataProvider.getData(name) as T?
companion object {
private val ourDataKeyIndex: ConcurrentMap<String, DataKey<*>> = ConcurrentHashMap()
@JvmStatic
fun <T> create(name: @NonNls String): DataKey<T> {
return ourDataKeyIndex.computeIfAbsent(name) { name: String -> DataKey<Any>(name) } as DataKey<T>
}
@JvmStatic
@ApiStatus.Internal
fun allKeys(): Array<DataKey<*>> = ourDataKeyIndex.values.toTypedArray()
@JvmStatic
@ApiStatus.Internal
fun allKeysCount(): Int = ourDataKeyIndex.size
}
}

View File

@@ -81,7 +81,7 @@ public final class SimpleDataContext extends CustomizedDataContext {
return this;
}
public @NotNull <T> Builder addNull(@NotNull DataKey<? super T> dataKey) {
public @NotNull Builder addNull(@NotNull DataKey<?> dataKey) {
if (myMap == null) myMap = new HashMap<>();
myMap.put(dataKey.getName(), EXPLICIT_NULL);
return this;

View File

@@ -372,9 +372,9 @@ class PreCachedDataContext implements AsyncDataContext, UserDataHolder, AnAction
@NotNull FList<ProviderData> cachedData) {
boolean noMap = sink.map == null;
DataSnapshot snapshot = new DataSnapshot() {
/** @noinspection unchecked*/
/** @noinspection unchecked */
@Override
public <T> @Nullable T get(@NotNull DataKey<@NotNull T> key) {
public <T> @Nullable T get(@NotNull DataKey<? extends @NotNull T> key) {
if (key == PlatformCoreDataKeys.CONTEXT_COMPONENT) return (T)component;
for (ProviderData map : cachedData) {
Object answer = map.uiSnapshot.get(key.getName());
@@ -409,7 +409,7 @@ class PreCachedDataContext implements AsyncDataContext, UserDataHolder, AnAction
FList<ProviderData> cachedDataForRules;
@Override
public <T> void set(@NotNull DataKey<@NotNull T> key, @Nullable T data) {
public <T> void set(@NotNull DataKey<? extends @NotNull T> key, @Nullable T data) {
if (data == null) return;
if (key == PlatformCoreDataKeys.CONTEXT_COMPONENT ||
key == PlatformCoreDataKeys.IS_MODAL_CONTEXT ||
@@ -439,7 +439,7 @@ class PreCachedDataContext implements AsyncDataContext, UserDataHolder, AnAction
}
@Override
public <T> void setNull(@NotNull DataKey<@NotNull T> key) {
public <T> void setNull(@NotNull DataKey<? extends @NotNull T> key) {
if (key == PlatformCoreDataKeys.CONTEXT_COMPONENT ||
key == PlatformCoreDataKeys.IS_MODAL_CONTEXT ||
key == PlatformDataKeys.MODALITY_STATE) {
@@ -450,7 +450,7 @@ class PreCachedDataContext implements AsyncDataContext, UserDataHolder, AnAction
}
@Override
public <T> void lazy(@NotNull DataKey<@NotNull T> key, @NotNull Function0<? extends @Nullable T> data) {
public <T> void lazy(@NotNull DataKey<? extends @NotNull T> key, @NotNull Function0<? extends @Nullable T> data) {
set(PlatformCoreDataKeys.BGT_DATA_PROVIDER, new MyLazy<>(key, data));
}
@@ -501,10 +501,10 @@ class PreCachedDataContext implements AsyncDataContext, UserDataHolder, AnAction
}
private static class MyLazy<T> implements DataProvider, DataValidators.SourceWrapper {
final DataKey<@NotNull T> key;
final DataKey<? extends @NotNull T> key;
final Function0<? extends @Nullable T> supplier;
MyLazy(@NotNull DataKey<@NotNull T> key, @NotNull Function0<? extends @Nullable T> supplier) {
MyLazy(@NotNull DataKey<? extends @NotNull T> key, @NotNull Function0<? extends @Nullable T> supplier) {
this.key = key;
this.supplier = supplier;
}

View File

@@ -1,47 +1,37 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.util;
package com.intellij.openapi.util
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
import org.jetbrains.annotations.ApiStatus
import java.util.function.Supplier
/**
* A key with string name which allows fetching or computing a typed value.
* @param <T> type of the value
*/
public interface ValueKey<T> {
</T> */
@ApiStatus.Obsolete
interface ValueKey<out T> {
/**
* @return the name of the key
*/
@NotNull String getName();
val name: String
/**
* Starts a matching chain using this key as the selector
* @return a matching chain object
*/
default @NotNull BeforeIf<T> match() {
return new ValueMatcherImpl<>(getName());
}
/**
* Starts a matching chain using the supplied key name as the selector
* @param name name to match
* @return a matching chain object
*/
static @NotNull BeforeIf<Object> match(@NotNull String name) {
return new ValueMatcherImpl<>(name);
fun match(): BeforeIf<T> {
return ValueMatcherImpl<T, Any>(name)
}
/**
* Represents a matching chain which matches a selector key against one or more "case" keys
* @param <T> type of the result
*/
interface BeforeIf<T> {
</T> */
interface BeforeIf<out T> {
/**
* @return the name of the key
*/
@NotNull String getKeyName();
val keyName: String
/**
* Continues a matching chain: the subsequent then-branch will take effect if
@@ -50,17 +40,17 @@ public interface ValueKey<T> {
* @param key "case" key to compare with selector key
* @param <TT> type of the "case" key
* @return a matching chain in the then-state (then-branches are accepted afterwards)
*/
@NotNull <TT> BeforeThen<T, TT> ifEq(@NotNull ValueKey<TT> key);
</TT> */
fun <TT> ifEq(key: ValueKey<TT>): BeforeThen<out T, TT>
/**
* Completes the matching chain returned the matched result
*
* @return the result of matching, supplied by the "then"-branch which follows the "case"-key matching the selector.
* Could be {@code null} if the corresponding "then"-branch produced a null value.
* Could be `null` if the corresponding "then"-branch produced a null value.
* @throws java.util.NoSuchElementException if no "case"-key matched the selector.
*/
T get();
fun get(): T
/**
* Completes the matching chain returned the matched result
@@ -68,14 +58,14 @@ public interface ValueKey<T> {
* @return the result of matching, supplied by the "then"-branch which follows the "case"-key matching the selector.
* Returns null if no "case"-key matched the selector, or the corresponding "then"-branch produced a null value.
*/
@Nullable T orNull();
fun orNull(): T?
}
/**
* Represents a matching chain in the "then-state"
* @param <T> type of the whole chain result
* @param <TT> type of the currently matched "case"-key value
*/
</TT></T> */
interface BeforeThen<T, TT> {
/**
* Adds an alternative "case"-key: the subsequent then-branch will take effect if
@@ -84,20 +74,32 @@ public interface ValueKey<T> {
* @param key an alternative "case"-key
* @return a matching chain in the then-state (then-branches are accepted afterwards)
*/
@NotNull BeforeThen<T, TT> or(@NotNull ValueKey<TT> key);
fun or(key: ValueKey<TT>): BeforeThen<T, TT>
/**
* Produces a value which will be returned as the chain result if the previous "case"-key matches the "selector"-key
* @param value value to be returned, could be null
* @return a matching chain in the if-state (if-branches or terminals are accepted afterwards)
*/
@NotNull BeforeIf<T> then(@Nullable TT value);
fun then(value: TT?): BeforeIf<T>
/**
* Produces a value which will be returned as the chain result if the previous "case"-key matches the "selector"-key
* @param fn a function to produce a value
* @return a matching chain in the if-state (if-branches or terminals are accepted afterwards)
*/
@NotNull BeforeIf<T> thenGet(@NotNull Supplier<? extends TT> fn);
fun thenGet(fn: Supplier<out TT>): BeforeIf<T>
}
companion object {
/**
* Starts a matching chain using the supplied key name as the selector
* @param name name to match
* @return a matching chain object
*/
@JvmStatic
fun match(name: String): BeforeIf<Object> {
return ValueMatcherImpl<Object, Any>(name)
}
}
}

View File

@@ -26,7 +26,7 @@ final class ValueMatcherImpl<T, T1> implements ValueKey.BeforeIf<T>, ValueKey.Be
}
@Override
public @NotNull <TT> ValueKey.BeforeThen<T, TT> ifEq(@NotNull ValueKey<TT> key) {
public <TT> ValueKey.@NotNull BeforeThen<? extends T, TT> ifEq(@NotNull ValueKey<? extends TT> key) {
switch (myState) {
case FINISHED:
throw new IllegalStateException("Matching is already finished");
@@ -78,7 +78,7 @@ final class ValueMatcherImpl<T, T1> implements ValueKey.BeforeIf<T>, ValueKey.Be
}
@Override
public @NotNull ValueKey.BeforeThen<T, T1> or(@NotNull ValueKey<T1> key) {
public ValueKey.@NotNull BeforeThen<T, T1> or(@NotNull ValueKey<? extends T1> key) {
switch (myState) {
case FINISHED:
throw new IllegalStateException("Matching is already finished");