KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 3-arg message.
23  */

24 public abstract class Message3 extends Message {
25
26   protected Message3(Type type, String JavaDoc fmt) {
27     super(type, fmt, 3);
28   }
29
30   protected TreeLogger branch3(TreeLogger logger, Object JavaDoc arg1, Object JavaDoc arg2,
31       Object JavaDoc arg3, Formatter fmt1, Formatter fmt2, Formatter fmt3,
32       Throwable JavaDoc caught) {
33     return logger.branch(type, compose3(arg1, arg2, arg3, fmt1, fmt2, fmt3),
34         caught);
35   }
36
37   protected String JavaDoc compose3(Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3,
38       Formatter fmt1, Formatter fmt2, Formatter fmt3) {
39
40     // Format the objects.
41
//
42
String JavaDoc stringArg1 = (arg1 != null ? fmt1.format(arg1) : "null");
43     String JavaDoc stringArg2 = (arg2 != null ? fmt2.format(arg2) : "null");
44     String JavaDoc stringArg3 = (arg3 != null ? fmt3.format(arg3) : "null");
45
46     // Decide how to order the inserts.
47
// Tests are biased toward $1..$2 order.
48
//
49
String JavaDoc insert1 = (argIndices[0] == 0) ? stringArg1 : ((argIndices[0] == 1)
50         ? stringArg2 : stringArg3);
51     String JavaDoc insert2 = (argIndices[1] == 1) ? stringArg2 : ((argIndices[1] == 0)
52         ? stringArg1 : stringArg3);
53     String JavaDoc insert3 = (argIndices[2] == 2) ? stringArg3 : ((argIndices[2] == 0)
54         ? stringArg1 : stringArg2);
55
56     // Cache the length of the inserts.
57
//
58
int lenInsert1 = insert1.length();
59     int lenInsert2 = insert2.length();
60     int lenInsert3 = insert3.length();
61
62     // Cache the length of each part.
63
//
64
int lenPart0 = fmtParts[0].length;
65     int lenPart1 = fmtParts[1].length;
66     int lenPart2 = fmtParts[2].length;
67     int lenPart3 = fmtParts[3].length;
68
69     // Prep for copying.
70
//
71
int dest = 0;
72     char[] chars = new char[minChars + lenInsert1 + lenInsert2 + lenInsert3];
73
74     // literal + insert, part 0
75
System.arraycopy(fmtParts[0], 0, chars, dest, lenPart0);
76     dest += lenPart0;
77
78     insert1.getChars(0, lenInsert1, chars, dest);
79     dest += lenInsert1;
80
81     // literal + insert, part 1
82
System.arraycopy(fmtParts[1], 0, chars, dest, lenPart1);
83     dest += lenPart1;
84
85     insert2.getChars(0, lenInsert2, chars, dest);
86     dest += lenInsert2;
87
88     // literal + insert, part 2
89
System.arraycopy(fmtParts[2], 0, chars, dest, lenPart2);
90     dest += lenPart2;
91
92     insert3.getChars(0, lenInsert3, chars, dest);
93     dest += lenInsert3;
94
95     // final literal
96
System.arraycopy(fmtParts[3], 0, chars, dest, lenPart3);
97
98     return new String JavaDoc(chars);
99   }
100
101   protected void log3(TreeLogger logger, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3,
102       Formatter fmt1, Formatter fmt2, Formatter fmt3, Throwable JavaDoc caught) {
103     if (logger.isLoggable(type)) {
104       logger.log(type, compose3(arg1, arg2, arg3, fmt1, fmt2, fmt3), caught);
105     }
106   }
107 }
108
Popular Tags