簡體   English   中英

比較枚舉條目-Java

[英]Comparing Enum Entries - Java

假設我有幾台機器:

//Test code of course.
public class Start{
  public static void main(String args[]){
    System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
    System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); 
    //The above should be true.
  }

  private enum Machine {
    BASIC_MACHINE (-1),
    BEVERAGE (-1),
    COFFEE(-1),
    COFFEE_GRINDER (5),
    COFFEE_MACHINE (6),
    GARDEN (-1),
    LAWN_MOWER (28);

    private final int catalogId;

    public int getCatalogId(){
      return catalogId;
    }

    public boolean isOfType(Machine to){
      return this == to;
    }

    Machine (int catalogId) {
      this.catalogId = catalogId;
    }
  }
}

在上面的示例中,有一些機器,它們會出現在目錄中,並且具有與它們關聯的ID號。 也有部分和部分。 因此,飲料機仍然是BASIC_MACHINE。 咖啡機仍然是飲料機。

程序中的某些功能在執行其功能之前,必須檢查本機是否實際上是BEFERAGE機。 目錄中的COFFEE_GRINDERs和COFFEE_MACHINEs都將簽出,並且該功能應通過。

我正在尋找的行為與instanceof或抽象類的繼承相當。 無論如何,COFFEE_MACHINE最終是BASIC_MACHINE的一種,我想檢查一下。

所以:

Machine.COFFEE_MACHINE isa Machine.COFFEE
Machine.BEVERAGE isa MACHINE.BASIC_MACHINE
Machine.LAWN_MOWER isa Machine.GARDEN == Machine.BASIC_MACHINE
Machine.COFFEE_MACHINE isnota Machine.COFFEE_GRINDER
Machine.LAWN_MOWER isnota Machine.COFFEE

一種可能的解決方案是使用回調和已保存的超類型來模仿某些繼承。 我很好奇這是否是最好的方法。

實現后,將如下所示:

//Test code of course.
public class Start{
  public static void main(String args[]){
    System.out.println(Machine.COFFEE_GRINDER.getCatalogId());
    System.out.println(Machine.COFFEE_MACHINE.isOfType(Machine.BASIC_MACHINE)); //Should be true.
  }

  private enum Machine {
    BASIC_MACHINE (-1),
    BEVERAGE (-1, BASIC_MACHINE),
    COFFEE(-1, BEVERAGE),
    COFFEE_GRINDER (5, COFFEE),
    COFFEE_MACHINE (6, COFFEE),
    GARDEN (-1, BASIC_MACHINE),
    LAWN_MOWER (28, GARDEN);

    private int catalogId;
    private Machine superMachine;

    public int getCatalogId(){
      return catalogId;
    }

    public Machine getSuperMachine(){
      return superMachine;
    }

    //With callback to superMachine (if present)
    public boolean isOfType(Machine to){
      return this == to || (getSuperMachine() != null && getSuperMachine().isOfType(to));
    }

    Machine (int catalogId) {
      this.catalogId = catalogId;
    }

    Machine (int catalogId, Machine superMachine) {
      this(catalogId);
      this.superMachine = superMachine;
    }
  }
}

如果我正確地理解了您的目標,則需要表達enum常量之間的關系網絡。 考慮到繼承不一定與您要表達的關系保持一致。 繼承模型的is-a類型之間的關系; 您需要實例之間的connects-to關系。 也許使用不同於“超級/繼承”的術語,例如“所有者-所有者”。 您可以在每個常量中嵌入一個指向owner實例的指針,並在靜態初始化器和構造函數中創建一個非循環有向圖結構。

public enum NetworkedMachine
{
BASIC_MACHINE(-1, null),
BEVERAGE(-1, BASIC_MACHINE),
COFFEE(-1, BASIC_MACHINE),
COFFEE_GRINDER(5, COFFEE),
COFFEE_MACHINE(6, COFFEE),
GARDEN(-1, BASIC_MACHINE),
LAWN_MOWER(28, GARDEN),
;

static final Map<NetworkedMachine, Set<NetworkedMachine>> owners;
static {
  Map<NetworkedMachine, Set<NetworkedMachine>> ownership = new HashMap<>();
  for (NetworkedMachine machine : values()) {
    ownership.put(machine, new HashSet<>());
  }
  for (NetworkedMachine machine : values()) {
    if (machine.owner != null) {
      ownership.get(machine.owner).add(machine);
    }
  }
  for (NetworkedMachine machine : values()) {
    Set<NetworkedMachine> owns = ownership.get(machine);
    ownership.put(machine, Collections.unmodifiableSet(owned));
  }
  owners = Collections.unmodifiableMap(ownership);
}

private final int catalogId;
private final NetworkedMachine owner;

NetworkedMachine(int catalogId, NetworkedMachine machine) {
  this.catalogId = catalogId;
  this.owner = machine;
}

public int getCatalogId() {
  return catalogId;
}

public NetworkedMachine getOwner() {
  return owner;
}

public Set<NetworkedMachine> getOwns() {
  return owners.get(this);
}

public boolean isOwned() {
  return owner != null;
}

}

測試輸出:

BASIC_MACHINE id: -1, owner: [null], owns: [GARDEN, BEVERAGE, COFFEE]
BEVERAGE id: -1, owner: [BASIC_MACHINE], owns: []
COFFEE id: -1, owner: [BASIC_MACHINE], owns: [COFFEE_GRINDER, COFFEE_MACHINE]
COFFEE_GRINDER id: 5, owner: [COFFEE], owns: []
COFFEE_MACHINE id: 6, owner: [COFFEE], owns: []
GARDEN id: -1, owner: [BASIC_MACHINE], owns: [LAWN_MOWER]
LAWN_MOWER id: 28, owner: [GARDEN], owns: []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM