枚举类的重写构造函数

时间:2019-04-18 10:45:50

标签: java

所以我是Java的新手,在这里需要一些帮助。

罐子里有一个类SqlDialect。该类看起来像这样,

public class SqlDialect {
.
.
public SqlDialect(Context context) {
    this.nullCollation = Objects.requireNonNull(context.nullCollation());
    this.dataTypeSystem = Objects.requireNonNull(context.dataTypeSystem());
    this.databaseProduct =
        Objects.requireNonNull(context.databaseProduct());
    String identifierQuoteString = context.identifierQuoteString();
    if (identifierQuoteString != null) {
      identifierQuoteString = identifierQuoteString.trim();
      if (identifierQuoteString.equals("")) {
        identifierQuoteString = null;
      }
    }
    this.identifierQuoteString = identifierQuoteString;
    this.identifierEndQuoteString =
        identifierQuoteString == null ? null
            : identifierQuoteString.equals("[") ? "]"
            : identifierQuoteString;
    this.identifierEscapedQuote =
        identifierQuoteString == null ? null
            : this.identifierEndQuoteString + this.identifierEndQuoteString;
  }

protected static Context emptyContext() {
    return new ContextImpl(DatabaseProduct.UNKNOWN, null, null, -1, -1, null,
        NullCollation.HIGH, RelDataTypeSystemImpl.DEFAULT, JethroDataSqlDialect.JethroInfo.EMPTY);
  } 
.
.
public enum DatabaseProduct {
    ACCESS("Access", "\"", NullCollation.HIGH),
    BIG_QUERY("Google BigQuery", "`", NullCollation.LOW),
    MSSQL("Microsoft SQL Server", "[", NullCollation.HIGH),
    MYSQL("MySQL", "`", NullCollation.LOW),
    ORACLE("Oracle", "\"", NullCollation.HIGH),
    DERBY("Apache Derby", null, NullCollation.HIGH),
    DB2("IBM DB2", null, NullCollation.HIGH);

    private final Supplier<SqlDialect> dialect;

    DatabaseProduct(String databaseProductName, String quoteString,
        NullCollation nullCollation) {
      Objects.requireNonNull(databaseProductName);
      Objects.requireNonNull(nullCollation);
      dialect = Suppliers.memoize(() -> {
        final SqlDialect dialect =
            SqlDialectFactoryImpl.simple(DatabaseProduct.this);
        if (dialect != null) {
          return dialect;
        }
        return new SqlDialect(SqlDialect.EMPTY_CONTEXT
            .withDatabaseProduct(DatabaseProduct.this)
            .withDatabaseProductName(databaseProductName)
            .withIdentifierQuoteString(quoteString)
            .withNullCollation(nullCollation));
      })::get;
    }

    public SqlDialect getDialect() {
      return dialect.get();
    }
  }

public interface Context {
    @Nonnull DatabaseProduct databaseProduct();
    Context withDatabaseProduct(@Nonnull DatabaseProduct databaseProduct);
    String databaseProductName();
    Context withDatabaseProductName(String databaseProductName);
    String databaseVersion();
    Context withDatabaseVersion(String databaseVersion);
    int databaseMajorVersion();
    Context withDatabaseMajorVersion(int databaseMajorVersion);
    int databaseMinorVersion();
    Context withDatabaseMinorVersion(int databaseMinorVersion);
    String identifierQuoteString();
    Context withIdentifierQuoteString(String identifierQuoteString);
    @Nonnull NullCollation nullCollation();
    Context withNullCollation(@Nonnull NullCollation nullCollation);
    @Nonnull RelDataTypeSystem dataTypeSystem();
    Context withDataTypeSystem(@Nonnull RelDataTypeSystem dataTypeSystem);
    JethroDataSqlDialect.JethroInfo jethroInfo();
    Context withJethroInfo(JethroDataSqlDialect.JethroInfo jethroInfo);
  }

  private static class ContextImpl implements Context {
    private final DatabaseProduct databaseProduct;
    private final String databaseProductName;
    private final String databaseVersion;
    private final int databaseMajorVersion;
    private final int databaseMinorVersion;
    private final String identifierQuoteString;
    private final NullCollation nullCollation;
    private final RelDataTypeSystem dataTypeSystem;
    private final JethroDataSqlDialect.JethroInfo jethroInfo;

    private ContextImpl(DatabaseProduct databaseProduct,
        String databaseProductName, String databaseVersion,
        int databaseMajorVersion, int databaseMinorVersion,
        String identifierQuoteString, NullCollation nullCollation,
        RelDataTypeSystem dataTypeSystem,
        JethroDataSqlDialect.JethroInfo jethroInfo) {
      this.databaseProduct = Objects.requireNonNull(databaseProduct);
      this.databaseProductName = databaseProductName;
      this.databaseVersion = databaseVersion;
      this.databaseMajorVersion = databaseMajorVersion;
      this.databaseMinorVersion = databaseMinorVersion;
      this.identifierQuoteString = identifierQuoteString;
      this.nullCollation = Objects.requireNonNull(nullCollation);
      this.dataTypeSystem = Objects.requireNonNull(dataTypeSystem);
      this.jethroInfo = Objects.requireNonNull(jethroInfo);
    }

    @Nonnull public DatabaseProduct databaseProduct() {
      return databaseProduct;
    }

    public Context withDatabaseProduct(
        @Nonnull DatabaseProduct databaseProduct) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    public String databaseProductName() {
      return databaseProductName;
    }

    public Context withDatabaseProductName(String databaseProductName) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    public String databaseVersion() {
      return databaseVersion;
    }

    public Context withDatabaseVersion(String databaseVersion) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    public int databaseMajorVersion() {
      return databaseMajorVersion;
    }

    public Context withDatabaseMajorVersion(int databaseMajorVersion) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    public int databaseMinorVersion() {
      return databaseMinorVersion;
    }

    public Context withDatabaseMinorVersion(int databaseMinorVersion) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    public String identifierQuoteString() {
      return identifierQuoteString;
    }

    public Context withIdentifierQuoteString(String identifierQuoteString) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    @Nonnull public NullCollation nullCollation() {
      return nullCollation;
    }

    public Context withNullCollation(@Nonnull NullCollation nullCollation) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    @Nonnull public RelDataTypeSystem dataTypeSystem() {
      return dataTypeSystem;
    }

    public Context withDataTypeSystem(@Nonnull RelDataTypeSystem dataTypeSystem) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }

    @Nonnull public JethroDataSqlDialect.JethroInfo jethroInfo() {
      return jethroInfo;
    }

    public Context withJethroInfo(JethroDataSqlDialect.JethroInfo jethroInfo) {
      return new ContextImpl(databaseProduct, databaseProductName,
          databaseVersion, databaseMajorVersion, databaseMinorVersion,
          identifierQuoteString, nullCollation, dataTypeSystem, jethroInfo);
    }
  } 
}

我可以从main()方法中调用此类的枚举类,如下所示:

public class Main {
public static void main(String[] args) throws Exception {
    SqlDialect dialect = SqlDialect.DatabaseProduct.MYSQL.getDialect();
}
}

现在,我想覆盖枚举类DatabaseProduct()的构造函数DatabaseProduct并给出自己的实现,即

DatabaseProduct(String databaseProductName, String quoteString,
        NullCollation nullCollation) {
      Objects.requireNonNull(databaseProductName);
      Objects.requireNonNull(nullCollation);
      dialect = Suppliers.memoize(() -> {
        final SqlDialect dialect =
            SqlDialectFactoryImpl2.getDialect(DatabaseProduct.this); //<--- This is what the change is in the overridden method 
        if (dialect != null) {
          return dialect;
        }
        return new SqlDialect(SqlDialect.EMPTY_CONTEXT
            .withDatabaseProduct(DatabaseProduct.this)
            .withDatabaseProductName(databaseProductName)
            .withIdentifierQuoteString(quoteString)
            .withNullCollation(nullCollation));
      })::get;
    }

那么有人可以告诉我该怎么做吗?非常感谢。谢谢。

0 个答案:

没有答案