KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > msg > Message2


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc fmt) {
27     super(type, fmt, 2);
28   }
29
30   protected TreeLogger branch2(TreeLogger logger, Object JavaDoc arg1, Object JavaDoc arg2,
31       Formatter fmt1, Formatter fmt2, Throwable JavaDoc caught) {
32     return logger.branch(type, compose2(arg1, arg2, fmt1, fmt2), caught);
33   }
34
35   protected String JavaDoc compose2(Object JavaDoc arg1, Object JavaDoc arg2, Formatter fmt1,
36       Formatter fmt2) {
37
38     // Format the objects.
39
//
40
String JavaDoc stringArg1 = (arg1 != null ? fmt1.format(arg1) : "null");
41     String JavaDoc stringArg2 = (arg2 != null ? fmt2.format(arg2) : "null");
42
43     // Decide how to order the inserts.
44
// Tests are biased toward $1..$2 order.
45
//
46
String JavaDoc insert1 = (argIndices[0] == 0) ? stringArg1 : stringArg2;
47     String JavaDoc insert2 = (argIndices[1] == 1) ? stringArg2 : stringArg1;
48
49     // Cache the length of the inserts.
50
//
51
int lenInsert1 = insert1.length();
52     int lenInsert2 = insert2.length();
53
54     // Cache the length of each part.
55
//
56
int lenPart0 = fmtParts[0].length;
57     int lenPart1 = fmtParts[1].length;
58     int lenPart2 = fmtParts[2].length;
59
60     // Prep for copying.
61
//
62
int dest = 0;
63     char[] chars = new char[minChars + lenInsert1 + lenInsert2];
64
65     // literal + insert, part 0
66
System.arraycopy(fmtParts[0], 0, chars, dest, lenPart0);
67     dest += lenPart0;
68
69     insert1.getChars(0, lenInsert1, chars, dest);
70     dest += lenInsert1;
71
72     // literal + insert, part 1
73
System.arraycopy(fmtParts[1], 0, chars, dest, lenPart1);
74     dest += lenPart1;
75
76     insert2.getChars(0, lenInsert2, chars, dest);
77     dest += lenInsert2;
78
79     // final literal
80
System.arraycopy(fmtParts[2], 0, chars, dest, lenPart2);
81
82     return new String JavaDoc(chars);
83   }
84
85   protected void log2(TreeLogger logger, Object JavaDoc arg1, Object JavaDoc arg2,
86       Formatter fmt1, Formatter fmt2, Throwable JavaDoc caught) {
87     if (logger.isLoggable(type)) {
88       logger.log(type, compose2(arg1, arg2, fmt1, fmt2), caught);
89     }
90   }
91 }
92
Popular Tags