package org.cass; import java.awt.*; import java.io.*; import java.util.regex.*; /** A class containing the default attributes of the three SMEI modes, as well as a set of utilities specific to modes. @author Zachary Vaughan @version 1.1, 07/01/2006 @see Camera Camera */ public final class Mode implements Serializable { private static final Pattern pat1 = Pattern.compile("c[123]m[012].*"), pat2 = Pattern.compile("c[123].*"); /** The unique instance representing Mode 0 (engineering mode, 1x1 data binning). */ public static final Mode M0 = new Mode(0, 1272, 256); /** The unique instance representing Mode 1 (science mode, 2x2 data binning). */ public static final Mode M1 = new Mode(1, 636, 128); /** The unique instance representing Mode 2 (science mode, 4x4 data binning). */ public static final Mode M2 = new Mode(2, 318, 64); /** Gets the Mode instance represented by the given integer. The integer can only be one of 0, 1 or 2. Any other value will result in an IllegalArgumentException. @param i the integer to interpret. @throws IllegalArgumentException if the integer is not one of 0, 1 or 2. @return the corresponding Mode instance of the given number. */ public static Mode getMode(int i) { if(i == 0) return M0; if(i == 1) return M1; if(i == 2) return M2; throw new IllegalArgumentException("Invalid mode number."); } /** Gets the Mode instance represented by the given string argument. The string argument can be any string beginning with optional whitespace followed by an optional set of two characters conforming to a Camera-parseable string followed by at least two characters, the first of which must be either an uppercase or lowercase "M" and followed by the number representing the desired mode ID. These last two characters are optional if a camera-parseable prefix is used, in which case the default Mode instance (represented by the parsed Camera instance's default mode) is returned. @param s the string to parse. @throws NullPointerException if s is null. @throws IllegalArgumentException if the given string is not formatted correctly. @return the unique Mode instance for the given string argument. @see Camera#parseCamera(String) Camera.parseCamera(s) */ public static Mode parseMode(String s) { s = s.trim().toLowerCase(); if(s.length() >= 4 && pat1.matcher(s).matches()) switch(s.charAt(3)) { case '0': return M0; case '1': return M1; case '2': return M2; default: return null; } else if(s.length() >= 2 && pat2.matcher(s).matches()) return Camera.parseCamera(s).getDefaultMode(); else if(s.startsWith("m0")) return M0; else if(s.startsWith("m1")) return M1; else if(s.startsWith("m2")) return M2; else throw new IllegalArgumentException("Invalid mode string format."); } private int id; private transient int dwidth; private transient int dheight; private Mode(int id, int dwidth, int dheight) { this.id = id; this.dwidth = dwidth; this.dheight = dheight; } /** Gets the unique ID associated with this mode, either 0, 1 or 2. @return the unique ID. */ public int getID() { return id; } /** Gets the default width in pixels of a SMEI frame in this mode. @return the default frame width in pixels. */ public int getDefaultFrameWidth() { return dwidth; } /** Gets the default height in pixels of a SMEI frame in this mode. @return the default frame height in pixels. */ public int getDefaultFrameHeight() { return dheight; } /** Gets the default size of a SMEI frame in this image as a Dimension. @return the default frame size in pixels. @see java.awt.Dimension Dimension */ public Dimension getDefaultFrameSize() { return new Dimension(dwidth, dheight); } /** Gets the String representation of this mode. The string consists of a lowercase "m" followed by the ID of this Mode. @return the string representation. */ public String toString() { return "m" + id; } private Object readResolve() throws ObjectStreamException { switch(id) { case 0: return M0; case 1: return M1; case 2: return M2; default: throw new InvalidObjectException("Invalid mode ID."); } } }