From 3ace7619726b80c2e2237a1e69f0c28274c2f6c3 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 23 Jul 2018 16:32:46 +0200 Subject: [PATCH] Cleanup (warnings; minor optimization) --- .../impl/CheckForUpdateResult.java | 23 +------- .../openapi/updateSettings/impl/UpdateInfo.kt | 56 +++++++++---------- 2 files changed, 28 insertions(+), 51 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/CheckForUpdateResult.java b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/CheckForUpdateResult.java index 369657b85c93..e7a26c3324f7 100644 --- a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/CheckForUpdateResult.java +++ b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/CheckForUpdateResult.java @@ -1,27 +1,11 @@ -/* - * Copyright 2000-2016 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. 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.updateSettings.impl; import com.intellij.openapi.util.BuildNumber; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.List; - public class CheckForUpdateResult { private final UpdateStrategy.State myState; private final BuildInfo myNewBuild; @@ -54,8 +38,7 @@ public class CheckForUpdateResult { @Nullable public PatchInfo findPatchForBuild(@NotNull BuildNumber build) { - List patches = myNewBuild != null ? myNewBuild.getPatches() : Collections.emptyList(); - return patches.stream().filter(p -> p.isAvailable() && p.getFromBuild().compareTo(build) == 0).findFirst().orElse(null); + return myNewBuild == null ? null : ContainerUtil.find(myNewBuild.getPatches(), p -> p.isAvailable() && p.getFromBuild().compareTo(build) == 0); } @Nullable diff --git a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateInfo.kt b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateInfo.kt index 5b4aeba84e84..40e05559e670 100644 --- a/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateInfo.kt +++ b/platform/platform-impl/src/com/intellij/openapi/updateSettings/impl/UpdateInfo.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. 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.updateSettings.impl import com.intellij.openapi.application.impl.ApplicationInfoImpl @@ -32,7 +18,7 @@ class UpdatesInfo(node: Element) { } class Product(node: Element) { - val name: String = node.getAttributeValue("name") ?: throw JDOMException("product@name missing") + val name: String = node.getMandatoryAttributeValue("name") val codes: Set = node.getChildren("code").map { it.value.trim() }.toSet() val channels: List = node.getChildren("channel").map(::UpdateChannel) @@ -45,7 +31,7 @@ class UpdateChannel(node: Element) { const val LICENSING_RELEASE: String = "release" } - val id: String = node.getAttributeValue("id") ?: throw JDOMException("channel@id missing") + val id: String = node.getMandatoryAttributeValue("id") val status: ChannelStatus = ChannelStatus.fromCode(node.getAttributeValue("status")) val licensing: String = node.getAttributeValue("licensing", LICENSING_RELEASE) val evalDays: Int = node.getAttributeValue("evalDays")?.toInt() ?: 30 @@ -55,7 +41,7 @@ class UpdateChannel(node: Element) { } class BuildInfo(node: Element) { - val number: BuildNumber = parseBuildNumber(node) + val number: BuildNumber = parseBuildNumber(node.getMandatoryAttributeValue("fullNumber", "number")) val apiVersion: BuildNumber = BuildNumber.fromStringWithProductCode(node.getAttributeValue("apiVersion"), number.productCode) ?: number val version: String = node.getAttributeValue("version") ?: "" val message: String = node.getChild("message")?.value ?: "" @@ -65,21 +51,23 @@ class BuildInfo(node: Element) { val buttons: List = node.getChildren("button").map(::ButtonInfo) val patches: List = node.getChildren("patch").map(::PatchInfo) - private fun parseBuildNumber(node: Element) = let { - val buildNumber = BuildNumber.fromString( - node.getAttributeValue("fullNumber") ?: node.getAttributeValue("number") ?: throw JDOMException("build@number missing")) - if (buildNumber.productCode.isNotEmpty()) buildNumber else BuildNumber(ApplicationInfoImpl.getShadowInstance().build.productCode, *buildNumber.components) + private fun parseBuildNumber(value: String): BuildNumber { + var buildNumber = BuildNumber.fromString(value) + if (buildNumber.productCode.isEmpty()) { + buildNumber = BuildNumber(ApplicationInfoImpl.getShadowInstance().build.productCode, *buildNumber.components) + } + return buildNumber } - private fun parseDate(value: String?): Date? = value?.let { - try { - SimpleDateFormat("yyyyMMdd", Locale.US).parse(it) // same as the 'majorReleaseDate' in ApplicationInfo.xml + private fun parseDate(value: String?): Date? = + if (value == null) null + else try { + SimpleDateFormat("yyyyMMdd", Locale.US).parse(value) // same as the 'majorReleaseDate' in ApplicationInfo.xml } catch (e: ParseException) { - Logger.getInstance(BuildInfo::class.java).info("Failed to parse build release date " + it) + Logger.getInstance(BuildInfo::class.java).info("invalid build release date: ${value}") null } - } val downloadUrl: String? get() = buttons.find(ButtonInfo::isDownload)?.url @@ -88,18 +76,24 @@ class BuildInfo(node: Element) { } class ButtonInfo(node: Element) { - val name: String = node.getAttributeValue("name") ?: throw JDOMException("button@name missing") - val url: String = node.getAttributeValue("url") ?: throw JDOMException("button@url missing") + val name: String = node.getMandatoryAttributeValue("name") + val url: String = node.getMandatoryAttributeValue("url") val isDownload: Boolean = node.getAttributeValue("download") != null // a button marked with this attribute is hidden when a patch is available override fun toString(): String = name } class PatchInfo(node: Element) { - val fromBuild: BuildNumber = BuildNumber.fromString(node.getAttributeValue("fullFrom") ?: node.getAttributeValue("from") ?: throw JDOMException("patch@from missing")) + val fromBuild: BuildNumber = BuildNumber.fromString(node.getMandatoryAttributeValue("fullFrom", "from")) val size: String? = node.getAttributeValue("size") val isAvailable: Boolean = node.getAttributeValue("exclusions")?.splitToSequence(",")?.none { it.trim() == osSuffix } ?: true val osSuffix: String get() = if (SystemInfo.isWindows) "win" else if (SystemInfo.isMac) "mac" else if (SystemInfo.isUnix) "unix" else "unknown" -} \ No newline at end of file +} + +private fun Element.getMandatoryAttributeValue(attribute: String) = + getAttributeValue(attribute) ?: throw JDOMException("${name}@${attribute} missing") + +private fun Element.getMandatoryAttributeValue(attribute: String, fallback: String) = + getAttributeValue(attribute) ?: getMandatoryAttributeValue(fallback) \ No newline at end of file