From 9879965407d8e65c6ad920b9fb913ece0c464edb Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 19 Nov 2013 14:54:33 +0400 Subject: [PATCH] Library functions --- .../org/jetbrains/jet/utils/collections.kt | 27 +++++++++++++++++++ .../src/org/jetbrains/jet/utils/coreLib.kt | 8 +++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 core/util.runtime/src/org/jetbrains/jet/utils/collections.kt diff --git a/core/util.runtime/src/org/jetbrains/jet/utils/collections.kt b/core/util.runtime/src/org/jetbrains/jet/utils/collections.kt new file mode 100644 index 00000000000..199d3fb575a --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/jet/utils/collections.kt @@ -0,0 +1,27 @@ +/* + * 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 java.util.LinkedHashMap + +public fun Iterable.toMap(key: (V) -> K): Map { + val map = LinkedHashMap() + for (v in this) { + map[key(v)] = v + } + return map +} \ No newline at end of file diff --git a/core/util.runtime/src/org/jetbrains/jet/utils/coreLib.kt b/core/util.runtime/src/org/jetbrains/jet/utils/coreLib.kt index 5a4922cabc3..bc32962de5c 100644 --- a/core/util.runtime/src/org/jetbrains/jet/utils/coreLib.kt +++ b/core/util.runtime/src/org/jetbrains/jet/utils/coreLib.kt @@ -21,4 +21,10 @@ fun Boolean.iif(then: T, _else: T): T = if (this) then else _else // These two functions are needed in conjunction with iif: // foo.eq(bar).iif(a, b) fun T.eq(eq: T): Boolean = this == eq -fun T.neq(eq: T): Boolean = this != eq \ No newline at end of file +fun T.neq(eq: T): Boolean = this != eq + +fun T?.sure(message: String): T { + if (this == null) + throw AssertionError(message) + return this +} \ No newline at end of file