787b1c2920b35d2c4e56a3bdfb69bb2000150ebc
[fleet.git] / src / edu / berkeley / fleet / api / Ship.java
1 package edu.berkeley.fleet.api;
2 import java.io.*;
3 import java.util.*;
4
5 public abstract class Ship {
6
7     /** return the Fleet that this Ship belongs to */
8     public abstract Fleet  getFleet();
9
10     /** returns the type of the ship ("Fetch", "ALU", etc) */
11     public abstract String getType();
12     
13     /** return all benkoboxes which feed this ship; order is NOT significant */
14     public abstract Iterable<BenkoBox> getBenkoBoxes();
15
16     public BenkoBox getBenkoBox(String s) {
17         for(BenkoBox b : getBenkoBoxes())
18             if (b.getName().equals(s))
19                 return b;
20         throw new RuntimeException("unknown port \""+getType()+"."+s+"\"");
21     }
22
23     public int getOrdinal() {
24         int ord = 0;
25         for(Ship s : getFleet()) {
26             if (s==this) return ord;
27             if (s.getType() != null && s.getType().equals(getType())) ord++;
28         }
29         throw new RuntimeException("inconsistency: Ship does not belong to its own Fleet!");
30     }
31
32     public String toString() { return getType() + "[" + getOrdinal() + "]"; }
33
34 }