001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.util.msg;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.TreeLogger.Type;
020:
021: /**
022: * 3-arg message.
023: */
024: public abstract class Message3 extends Message {
025:
026: protected Message3(Type type, String fmt) {
027: super (type, fmt, 3);
028: }
029:
030: protected TreeLogger branch3(TreeLogger logger, Object arg1,
031: Object arg2, Object arg3, Formatter fmt1, Formatter fmt2,
032: Formatter fmt3, Throwable caught) {
033: return logger.branch(type, compose3(arg1, arg2, arg3, fmt1,
034: fmt2, fmt3), caught);
035: }
036:
037: protected String compose3(Object arg1, Object arg2, Object arg3,
038: Formatter fmt1, Formatter fmt2, Formatter fmt3) {
039:
040: // Format the objects.
041: //
042: String stringArg1 = (arg1 != null ? fmt1.format(arg1) : "null");
043: String stringArg2 = (arg2 != null ? fmt2.format(arg2) : "null");
044: String stringArg3 = (arg3 != null ? fmt3.format(arg3) : "null");
045:
046: // Decide how to order the inserts.
047: // Tests are biased toward $1..$2 order.
048: //
049: String insert1 = (argIndices[0] == 0) ? stringArg1
050: : ((argIndices[0] == 1) ? stringArg2 : stringArg3);
051: String insert2 = (argIndices[1] == 1) ? stringArg2
052: : ((argIndices[1] == 0) ? stringArg1 : stringArg3);
053: String insert3 = (argIndices[2] == 2) ? stringArg3
054: : ((argIndices[2] == 0) ? stringArg1 : stringArg2);
055:
056: // Cache the length of the inserts.
057: //
058: int lenInsert1 = insert1.length();
059: int lenInsert2 = insert2.length();
060: int lenInsert3 = insert3.length();
061:
062: // Cache the length of each part.
063: //
064: int lenPart0 = fmtParts[0].length;
065: int lenPart1 = fmtParts[1].length;
066: int lenPart2 = fmtParts[2].length;
067: int lenPart3 = fmtParts[3].length;
068:
069: // Prep for copying.
070: //
071: int dest = 0;
072: char[] chars = new char[minChars + lenInsert1 + lenInsert2
073: + lenInsert3];
074:
075: // literal + insert, part 0
076: System.arraycopy(fmtParts[0], 0, chars, dest, lenPart0);
077: dest += lenPart0;
078:
079: insert1.getChars(0, lenInsert1, chars, dest);
080: dest += lenInsert1;
081:
082: // literal + insert, part 1
083: System.arraycopy(fmtParts[1], 0, chars, dest, lenPart1);
084: dest += lenPart1;
085:
086: insert2.getChars(0, lenInsert2, chars, dest);
087: dest += lenInsert2;
088:
089: // literal + insert, part 2
090: System.arraycopy(fmtParts[2], 0, chars, dest, lenPart2);
091: dest += lenPart2;
092:
093: insert3.getChars(0, lenInsert3, chars, dest);
094: dest += lenInsert3;
095:
096: // final literal
097: System.arraycopy(fmtParts[3], 0, chars, dest, lenPart3);
098:
099: return new String(chars);
100: }
101:
102: protected void log3(TreeLogger logger, Object arg1, Object arg2,
103: Object arg3, Formatter fmt1, Formatter fmt2,
104: Formatter fmt3, Throwable caught) {
105: if (logger.isLoggable(type)) {
106: logger.log(type, compose3(arg1, arg2, arg3, fmt1, fmt2,
107: fmt3), caught);
108: }
109: }
110: }
|