001: /*
002: * $RCSfile: StringSegment.java,v $
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: /**
029: * Represents String segment in an animation.
030: *
031: * @version $Id: StringSegment.java,v 1.3 2006/06/29 10:47:34 ln156897 Exp $
032: */
033: class StringSegment implements Segment {
034: /**
035: * The segment's begin value.
036: */
037: String[] start;
038:
039: /**
040: * The segment's end value.
041: */
042: String[] end;
043:
044: /**
045: * @return the start value.
046: */
047: public Object[] getStart() {
048: return start;
049: }
050:
051: /**
052: * @return set end value.
053: */
054: public Object[] getEnd() {
055: return end;
056: }
057:
058: /**
059: * Sets the start value to its notion of 'zero'.
060: * For a StringSegment, a 'zero' start means empty strings
061: * on all components.
062: */
063: public void setZeroStart() {
064: for (int i = 0; i < start.length; i++) {
065: start[i] = "";
066: }
067: }
068:
069: /**
070: * Sets the start value.
071: *
072: * @param newStart the new segment start value.
073: */
074: public void setStart(Object[] newStart) {
075: start = (String[]) newStart;
076: }
077:
078: /**
079: * Collapses this segment with the one passed as a parameter.
080: * Note that if the input segment is not of the same class
081: * as this one, an IllegalArgumentException is thrown. The
082: * method also throws an exception if the input segment's
083: * end does not have the same number of components as this
084: * segment's end.
085: *
086: * After this method is called, this segment's end value
087: * is the one of the input <code>seg</code> parameter.
088: *
089: * @param seg the Segment to collapse with this one.
090: * @param anim the Animation this segment is part of.
091: */
092: public void collapse(final Segment seg, final Animation anim) {
093: StringSegment mseg = (StringSegment) seg;
094: if (mseg.end.length != end.length) {
095: throw new IllegalArgumentException();
096: }
097:
098: end = mseg.end;
099: }
100:
101: /**
102: * Adds the input value to this Segment's end value.
103: *
104: * @param by the value to add. Throws IllegalArgumentException if this
105: * Segment type is not additive or if the input value is incompatible (e.g.,
106: * different number of components or different number of dimensions on a
107: * component).
108: */
109: public void addToEnd(Object[] by) {
110: throw new IllegalArgumentException();
111: }
112:
113: /**
114: * @return true if this segment type supports addition. false
115: * otherwise.
116: */
117: public boolean isAdditive() {
118: return false;
119: }
120:
121: /**
122: * Computes an interpolated value for the given penetration in the
123: * segment.
124: *
125: * @param p the segment penetration. Should be in the [0, 1] range.
126: * @return the interpolated value.
127: */
128: public String[] compute(final float p) {
129: if (p == 1) {
130: return end;
131: } else {
132: return start;
133: }
134: }
135:
136: /**
137: * Debug helper.
138: */
139: public String toString() {
140: StringBuffer sb = new StringBuffer();
141: sb.append("StringSegment[");
142: if (start == null) {
143: sb.append("null");
144: } else {
145: sb.append("start[" + start.length + "] : {");
146: for (int ci = 0; ci < start.length; ci++) {
147: sb.append("\"" + start[ci] + "\"");
148: if (ci < start.length - 1) {
149: sb.append(",");
150: }
151: }
152: sb.append("}");
153: }
154:
155: if (end == null) {
156: sb.append(" null");
157: } else {
158: sb.append(" end[" + end.length + "] : {");
159: for (int ci = 0; ci < end.length; ci++) {
160: sb.append("\"" + end[ci] + "\"");
161: if (ci < end.length - 1) {
162: sb.append(",");
163: }
164: }
165: sb.append("}");
166: }
167:
168: return sb.toString();
169: }
170:
171: /**
172: * Computes this segment's length. This is always the value
173: * '1' for a string segment.
174: */
175: public final float getLength() {
176: return 1;
177: }
178:
179: /**
180: * Should be called after the segment's configuration is complete
181: * to give the segment's implementation a chance to initialize
182: * internal data and cache values.
183: */
184: public final void initialize() {
185: }
186:
187: }
|