net.kolls.railworld.car
Class CachedCar

java.lang.Object
  extended by net.kolls.railworld.car.CachedCar
All Implemented Interfaces:
Car, SaveLoad

public class CachedCar
extends java.lang.Object
implements Car

A cached car is useful for cars created by scripts. To avoid a significant performance hit, scripts should wrap their cars in cached cars when possible. A cached car caches: canUserCreate, isEngine, isLoadable, color, segs, length, midColor, show, weight, loaded.
These values are gathered when the cached car is created, and also on a Car.load() Car.unload() or SaveLoad.load(Map).

Author:
Steve Kollmansberger

Field Summary
 
Fields inherited from interface net.kolls.railworld.Car
CAR_WIDTH, DIST_BETWEEN_CARS
 
Constructor Summary
CachedCar(Car c)
          Create a cached car.
 
Method Summary
 boolean canUserCreate()
          Indicates if this car should appear in any user creation selection window.
 java.awt.Color color()
          Return the Car's color.
 boolean isEngine()
          Indicates if this car provides power.
 boolean isLoadable()
          Determine if the current Car can be loaded and unloaded; that is, does it carry cargo.
 Distance length()
          Returns the length of this Car.
 void load()
          Loads the current Car.
 void load(java.util.Map<java.lang.String,java.lang.String> data)
          Load from saved.
 boolean loaded()
          Indicates if the Car is currently loaded.
 java.awt.Color midColor()
          Indicates what the middle line (loaded) color should be.
 java.lang.Object newInstance()
          like getClass().newInstance().
 java.util.Map<java.lang.String,java.lang.String> save()
          Store all persistent data into a map and return for saving.
 java.util.Set<RailSegment> segs()
          For the current Car, returns the segments that the Car occupies, if any.
 java.lang.String show()
          Gives a human-readable word for the Car.
 java.lang.String toString()
           
 void unload()
          Unloads the current Car.
 int weight()
          Returns the weight of this Car (taking load/unload into account, if necessary) in US Tons.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CachedCar

public CachedCar(Car c)
Create a cached car.

Parameters:
c - Car to cache
Method Detail

canUserCreate

public boolean canUserCreate()
Description copied from interface: Car
Indicates if this car should appear in any user creation selection window.

Specified by:
canUserCreate in interface Car
Returns:
true if the car is user creatable

color

public java.awt.Color color()
Description copied from interface: Car
Return the Car's color. The following color assignments exist: black: open hopper car blue: tank car cyan: intermodal dark gray: engine gray: green: covered hopper light gray: passenger magenta: auto rack orange: centerbeam flat car pink: boxcar red: caboose white: yellow: stock car

Specified by:
color in interface Car
Returns:
The Color of the current Car.

isEngine

public boolean isEngine()
Description copied from interface: Car
Indicates if this car provides power.

Specified by:
isEngine in interface Car
Returns:
true if the car provides power (is a working engine)

isLoadable

public boolean isLoadable()
Description copied from interface: Car
Determine if the current Car can be loaded and unloaded; that is, does it carry cargo.

Specified by:
isLoadable in interface Car
Returns:
true if the Car may be loaded and unloaded. Defaults to true.

length

public Distance length()
Description copied from interface: Car
Returns the length of this Car.

Specified by:
length in interface Car
Returns:
Distance indicating the length of the Car.

load

public void load()
Description copied from interface: Car
Loads the current Car. Do not call on a Car you have specified as not loadable.

Specified by:
load in interface Car

loaded

public boolean loaded()
Description copied from interface: Car
Indicates if the Car is currently loaded.

Specified by:
loaded in interface Car
Returns:
true if the car is loadable and is currently loaded.

midColor

public java.awt.Color midColor()
Description copied from interface: Car
Indicates what the middle line (loaded) color should be. Used by TrainPainter.

Specified by:
midColor in interface Car
Returns:
The Color of the middle line in the car

segs

public java.util.Set<RailSegment> segs()
Description copied from interface: Car
For the current Car, returns the segments that the Car occupies, if any. This should be considered read-only.

Specified by:
segs in interface Car
Returns:
Set of RailSegments.

show

public java.lang.String show()
Description copied from interface: Car
Gives a human-readable word for the Car. Should indicate only the type of the Car, not whether it is loaded or unloaded.

Specified by:
show in interface Car
Returns:
String indicating type of Car.

unload

public void unload()
Description copied from interface: Car
Unloads the current Car. Do not call on a Car you have specified as not loadable.

Specified by:
unload in interface Car

weight

public int weight()
Description copied from interface: Car
Returns the weight of this Car (taking load/unload into account, if necessary) in US Tons. 1 US Ton = 2000 US Pounds

Specified by:
weight in interface Car
Returns:
int representing weight in tons.

load

public void load(java.util.Map<java.lang.String,java.lang.String> data)
Description copied from interface: SaveLoad
Load from saved.

Specified by:
load in interface SaveLoad
Parameters:
data - Key value pairs saved by an instance of this class

newInstance

public java.lang.Object newInstance()
Description copied from interface: SaveLoad
like getClass().newInstance(). Allows script objects to also be copied.

Specified by:
newInstance in interface SaveLoad
Returns:
A new instance of this class

save

public java.util.Map<java.lang.String,java.lang.String> save()
Description copied from interface: SaveLoad
Store all persistent data into a map and return for saving. May return null if there is no data to save. This will be treated the same as the empty map.

Specified by:
save in interface SaveLoad
Returns:
A list of key-value pairs to be saved.

toString

public java.lang.String toString()
Specified by:
toString in interface SaveLoad
Overrides:
toString in class java.lang.Object
Returns:
Give the name of this type so it can be reconstructed by the factory.