001/* 002 * ModeShape (http://www.modeshape.org) 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 */ 016package org.modeshape.common.database; 017 018import java.util.Objects; 019import org.modeshape.common.util.CheckArg; 020 021/** 022 * Enum for a list of known databases. 023 * 024 * @author Horia Chiorean (hchiorea@redhat.com) 025 * @since 5.0 026 */ 027public class DatabaseType implements Comparable<DatabaseType> { 028 029 public static DatabaseType UNKNOWN = new DatabaseType(Name.UNKNOWN, 0, 0); 030 031 public enum Name { 032 MYSQL, 033 POSTGRES, 034 DERBY, 035 HSQL, 036 H2, 037 SQLITE, 038 DB2, 039 DB2_390, 040 INFORMIX, 041 INTERBASE, 042 FIREBIRD, 043 SQLSERVER, 044 ACCESS, 045 ORACLE, 046 SYBASE, 047 CASSANDRA, 048 UNKNOWN; 049 } 050 051 private final int minorVersion; 052 private final int majorVersion; 053 private final Name name; 054 055 /** 056 * Create a new instance which has a name, major and minor version. 057 * 058 * @param name the name of the database; may not be null 059 * @param majorVersion the major version; must be equal to or greater than 0 060 * @param minorVersion the minor version; must be equal to or greater than 0 061 */ 062 public DatabaseType(Name name, int majorVersion, int minorVersion) { 063 this.name = Objects.requireNonNull(name); 064 CheckArg.isNonNegative(majorVersion, "majorVersion"); 065 this.minorVersion = minorVersion; 066 CheckArg.isNonNegative(minorVersion, "minorVersion"); 067 this.majorVersion = majorVersion; 068 } 069 070 /** 071 * Get the DB minor version 072 * 073 * @return an integer 074 */ 075 public int minorVersion() { 076 return minorVersion; 077 } 078 079 /** 080 * Get the DB major version 081 * 082 * @return an integer 083 */ 084 public int majorVersion() { 085 return majorVersion; 086 } 087 088 /** 089 * Get the name of the database as a string 090 * 091 * @return the name, never {@code null} 092 */ 093 public String nameString() { 094 return name.name(); 095 } 096 097 /** 098 * Get the name of the database 099 * 100 * @return a {@link org.modeshape.common.database.DatabaseType.Name}, never {@code null} 101 */ 102 public Name name() { 103 return name; 104 } 105 106 @Override 107 public boolean equals(Object o) { 108 if (this == o) { 109 return true; 110 } 111 if (o == null || getClass() != o.getClass()) { 112 return false; 113 } 114 DatabaseType that = (DatabaseType) o; 115 return Objects.equals(minorVersion, that.minorVersion) && 116 Objects.equals(majorVersion, that.majorVersion) && 117 Objects.equals(name, that.name); 118 } 119 120 @Override 121 public int hashCode() { 122 return Objects.hash(minorVersion, majorVersion, name); 123 } 124 125 @Override 126 public int compareTo(DatabaseType other) { 127 int nameComparison = this.name.compareTo(other.name); 128 if (nameComparison != 0) { 129 return nameComparison; 130 } 131 int majorVersionComparison = Integer.compare(this.majorVersion, other.majorVersion); 132 if (majorVersionComparison != 0) { 133 return majorVersionComparison; 134 } 135 int minorVersionComparison = Integer.compare(this.minorVersion, other.minorVersion); 136 if (minorVersionComparison != 0) { 137 return minorVersionComparison; 138 } 139 return 0; 140 } 141 142 @Override 143 public String toString() { 144 return "Database[name=" + name + ", majorVersion=" + majorVersion + ", minorVersion=" + minorVersion + "]"; 145 } 146}