01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.util.msg;
17:
18: import com.google.gwt.core.ext.TreeLogger;
19: import com.google.gwt.core.ext.TreeLogger.Type;
20:
21: /**
22: * 2-arg message.
23: */
24: public abstract class Message2 extends Message {
25:
26: public Message2(Type type, String fmt) {
27: super (type, fmt, 2);
28: }
29:
30: protected TreeLogger branch2(TreeLogger logger, Object arg1,
31: Object arg2, Formatter fmt1, Formatter fmt2,
32: Throwable caught) {
33: return logger.branch(type, compose2(arg1, arg2, fmt1, fmt2),
34: caught);
35: }
36:
37: protected String compose2(Object arg1, Object arg2, Formatter fmt1,
38: Formatter fmt2) {
39:
40: // Format the objects.
41: //
42: String stringArg1 = (arg1 != null ? fmt1.format(arg1) : "null");
43: String stringArg2 = (arg2 != null ? fmt2.format(arg2) : "null");
44:
45: // Decide how to order the inserts.
46: // Tests are biased toward $1..$2 order.
47: //
48: String insert1 = (argIndices[0] == 0) ? stringArg1 : stringArg2;
49: String insert2 = (argIndices[1] == 1) ? stringArg2 : stringArg1;
50:
51: // Cache the length of the inserts.
52: //
53: int lenInsert1 = insert1.length();
54: int lenInsert2 = insert2.length();
55:
56: // Cache the length of each part.
57: //
58: int lenPart0 = fmtParts[0].length;
59: int lenPart1 = fmtParts[1].length;
60: int lenPart2 = fmtParts[2].length;
61:
62: // Prep for copying.
63: //
64: int dest = 0;
65: char[] chars = new char[minChars + lenInsert1 + lenInsert2];
66:
67: // literal + insert, part 0
68: System.arraycopy(fmtParts[0], 0, chars, dest, lenPart0);
69: dest += lenPart0;
70:
71: insert1.getChars(0, lenInsert1, chars, dest);
72: dest += lenInsert1;
73:
74: // literal + insert, part 1
75: System.arraycopy(fmtParts[1], 0, chars, dest, lenPart1);
76: dest += lenPart1;
77:
78: insert2.getChars(0, lenInsert2, chars, dest);
79: dest += lenInsert2;
80:
81: // final literal
82: System.arraycopy(fmtParts[2], 0, chars, dest, lenPart2);
83:
84: return new String(chars);
85: }
86:
87: protected void log2(TreeLogger logger, Object arg1, Object arg2,
88: Formatter fmt1, Formatter fmt2, Throwable caught) {
89: if (logger.isLoggable(type)) {
90: logger.log(type, compose2(arg1, arg2, fmt1, fmt2), caught);
91: }
92: }
93: }
|