MoreKeySpec.java revision 639bf62e4fc12c4f75ed0491512f6ed9d2fc2432
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.keyboard.internal;
18
19import android.text.TextUtils;
20
21import com.android.inputmethod.latin.Constants;
22import com.android.inputmethod.latin.utils.StringUtils;
23
24import java.util.Locale;
25
26public final class MoreKeySpec {
27    public final int mCode;
28    public final String mLabel;
29    public final String mOutputText;
30    public final int mIconId;
31
32    public MoreKeySpec(final String moreKeySpec, boolean needsToUpperCase, final Locale locale,
33            final KeyboardCodesSet codesSet) {
34        mLabel = StringUtils.toUpperCaseOfStringForLocale(
35                KeySpecParser.getLabel(moreKeySpec), needsToUpperCase, locale);
36        final int code = StringUtils.toUpperCaseOfCodeForLocale(
37                KeySpecParser.getCode(moreKeySpec, codesSet), needsToUpperCase, locale);
38        if (code == Constants.CODE_UNSPECIFIED) {
39            // Some letter, for example German Eszett (U+00DF: "ß"), has multiple characters
40            // upper case representation ("SS").
41            mCode = Constants.CODE_OUTPUT_TEXT;
42            mOutputText = mLabel;
43        } else {
44            mCode = code;
45            mOutputText = StringUtils.toUpperCaseOfStringForLocale(
46                    KeySpecParser.getOutputText(moreKeySpec), needsToUpperCase, locale);
47        }
48        mIconId = KeySpecParser.getIconId(moreKeySpec);
49    }
50
51    @Override
52    public int hashCode() {
53        int hashCode = 1;
54        hashCode = 31 + mCode;
55        hashCode = hashCode * 31 + mIconId;
56        hashCode = hashCode * 31 + (mLabel == null ? 0 : mLabel.hashCode());
57        hashCode = hashCode * 31 + (mOutputText == null ? 0 : mOutputText.hashCode());
58        return hashCode;
59    }
60
61    @Override
62    public boolean equals(final Object o) {
63        if (this == o) return true;
64        if (o instanceof MoreKeySpec) {
65            final MoreKeySpec other = (MoreKeySpec)o;
66            return mCode == other.mCode
67                    && mIconId == other.mIconId
68                    && TextUtils.equals(mLabel, other.mLabel)
69                    && TextUtils.equals(mOutputText, other.mOutputText);
70        }
71        return false;
72    }
73
74    @Override
75    public String toString() {
76        final String label = (mIconId == KeyboardIconsSet.ICON_UNDEFINED ? mLabel
77                : KeySpecParser.PREFIX_ICON + KeyboardIconsSet.getIconName(mIconId));
78        final String output = (mCode == Constants.CODE_OUTPUT_TEXT ? mOutputText
79                : Constants.printableCode(mCode));
80        if (StringUtils.codePointCount(label) == 1 && label.codePointAt(0) == mCode) {
81            return output;
82        } else {
83            return label + "|" + output;
84        }
85    }
86}
87