001: // Transmogrify License
002: //
003: // Copyright (c) 2001, ThoughtWorks, Inc.
004: // All rights reserved.
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions
007: // are met:
008: // - Redistributions of source code must retain the above copyright notice,
009: // this list of conditions and the following disclaimer.
010: // - Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // Neither the name of the ThoughtWorks, Inc. nor the names of its
014: // contributors may be used to endorse or promote products derived from this
015: // software without specific prior written permission.
016: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: public class PrimitiveClasses {
034:
035: public static final ExternalClass BOOLEAN = new ExternalClass(
036: Boolean.TYPE);
037: public static final ExternalClass CHAR = new ExternalClass(
038: Character.TYPE);
039: public static final ExternalClass BYTE = new ExternalClass(
040: Byte.TYPE);
041: public static final ExternalClass SHORT = new ExternalClass(
042: Short.TYPE);
043: public static final ExternalClass INT = new ExternalClass(
044: Integer.TYPE);
045: public static final ExternalClass LONG = new ExternalClass(
046: Long.TYPE);
047: public static final ExternalClass FLOAT = new ExternalClass(
048: Float.TYPE);
049: public static final ExternalClass DOUBLE = new ExternalClass(
050: Double.TYPE);
051:
052: private static List order;
053:
054: static {
055: order = new ArrayList();
056: order.add(DOUBLE);
057: order.add(FLOAT);
058: order.add(LONG);
059: order.add(INT);
060: order.add(SHORT);
061: order.add(BYTE);
062: }
063:
064: public static boolean typesAreCompatible(ExternalClass hole,
065: ExternalClass peg) {
066: boolean result = false;
067:
068: if (hole.equals(BOOLEAN)) {
069: result = peg.equals(BOOLEAN);
070: } else if (hole.equals(CHAR)) {
071: result = peg.equals(CHAR);
072: } else if (peg.equals(CHAR)) {
073: result = (hole.equals(CHAR) || order.indexOf(hole) <= order
074: .indexOf(INT));
075: } else {
076: result = (order.indexOf(hole) <= order.indexOf(peg));
077: }
078:
079: return result;
080: }
081:
082: public static IClass unaryPromote(IClass type) {
083: IClass result = type;
084:
085: if (type.equals(BYTE) || type.equals(SHORT)
086: || type.equals(CHAR)) {
087: result = INT;
088: }
089:
090: return result;
091: }
092:
093: public static IClass binaryPromote(IClass a, IClass b) {
094: IClass result = null;
095:
096: if (a.equals(DOUBLE) || b.equals(DOUBLE)) {
097: result = DOUBLE;
098: } else if (a.equals(FLOAT) || b.equals(FLOAT)) {
099: result = FLOAT;
100: } else if (a.equals(LONG) || b.equals(LONG)) {
101: result = LONG;
102: } else {
103: result = INT;
104: }
105:
106: return result;
107: }
108:
109: }
|