001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.system; 018 019import java.util.Arrays; 020import java.util.Collections; 021import java.util.List; 022 023import org.springframework.util.ClassUtils; 024 025/** 026 * Supported Java versions. 027 * 028 * @author Oliver Gierke 029 * @author Phillip Webb 030 * @since 2.0.0 031 */ 032public enum JavaVersion { 033 034 /** 035 * Java 1.8. 036 */ 037 EIGHT("1.8", "java.util.function.Function"), 038 039 /** 040 * Java 1.9. 041 */ 042 NINE("1.9", "java.security.cert.URICertStoreParameters"); 043 044 private final String name; 045 046 private final boolean available; 047 048 JavaVersion(String name, String className) { 049 this.name = name; 050 this.available = ClassUtils.isPresent(className, getClass().getClassLoader()); 051 } 052 053 @Override 054 public String toString() { 055 return this.name; 056 } 057 058 /** 059 * Returns the {@link JavaVersion} of the current runtime. 060 * @return the {@link JavaVersion} 061 */ 062 public static JavaVersion getJavaVersion() { 063 List<JavaVersion> candidates = Arrays.asList(JavaVersion.values()); 064 Collections.reverse(candidates); 065 for (JavaVersion candidate : candidates) { 066 if (candidate.available) { 067 return candidate; 068 } 069 } 070 return EIGHT; 071 } 072 073 /** 074 * Return if this version is equal to or newer than a given version. 075 * @param version the version to compare 076 * @return {@code true} if this version is equal to or newer than {@code version} 077 */ 078 public boolean isEqualOrNewerThan(JavaVersion version) { 079 return compareTo(version) >= 0; 080 } 081 082 /** 083 * Return if this version is older than a given version. 084 * @param version the version to compare 085 * @return {@code true} if this version is older than {@code version} 086 */ 087 public boolean isOlderThan(JavaVersion version) { 088 return compareTo(version) < 0; 089 } 090 091}