001: /*
002: * Copyright 2007 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.core.ext.typeinfo;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: class MetaData implements HasMetaData {
026:
027: /**
028: * Copy metadata from one metadata container to another. Perhaps overly
029: * paranoid but HasMetaData elements are mutable.
030: *
031: * @param dest
032: * @param src
033: */
034: static void copy(HasMetaData dest, HasMetaData src) {
035: String[] tagNames = src.getMetaDataTags();
036: for (String tagName : tagNames) {
037: String[][] tagValueSets = src.getMetaData(tagName);
038: for (String[] tagValues : tagValueSets) {
039: dest.addMetaData(tagName, tagValues);
040: }
041: }
042: }
043:
044: private final Map<String, List<String[]>> tagNameToStringArrayList = new HashMap<String, List<String[]>>();
045:
046: public void addMetaData(String tagName, String[] values) {
047: List<String[]> list = tagNameToStringArrayList.get(tagName);
048: if (list == null) {
049: list = new ArrayList<String[]>();
050: tagNameToStringArrayList.put(tagName, list);
051: }
052: // Yes, we're adding the string array as an object into the list.
053: //
054: list.add(values);
055: }
056:
057: public String[][] getMetaData(String tagName) {
058: List<String[]> list = tagNameToStringArrayList.get(tagName);
059: if (list != null) {
060: return list.toArray(TypeOracle.NO_STRING_ARR_ARR);
061: } else {
062: return TypeOracle.NO_STRING_ARR_ARR;
063: }
064: }
065:
066: public String[] getMetaDataTags() {
067: return tagNameToStringArrayList.keySet().toArray(
068: TypeOracle.NO_STRINGS);
069: }
070:
071: @Override
072: public String toString() {
073: StringBuffer sb = new StringBuffer();
074: final Set<String> keys = tagNameToStringArrayList.keySet();
075: String[] tags = keys.toArray(TypeOracle.NO_STRINGS);
076: Arrays.sort(tags);
077: for (int i = 0; i < tags.length; i++) {
078: sb.append(tags[i]);
079: sb.append(" => ");
080:
081: boolean isFirstList = true;
082: List<String[]> stringArrayList = tagNameToStringArrayList
083: .get(tags[i]);
084: for (String[] values : stringArrayList) {
085: if (isFirstList) {
086: isFirstList = false;
087: } else {
088: sb.append(", ");
089: }
090: sb.append('[');
091: boolean needComma = false;
092: for (int j = 0; j < values.length; j++) {
093: if (needComma) {
094: sb.append(", ");
095: } else {
096: needComma = true;
097: }
098: sb.append(values[j]);
099: }
100: sb.append(']');
101: }
102: }
103: return sb.toString();
104: }
105:
106: }
|