Example usage for java.lang CharSequence hashCode

List of usage examples for java.lang CharSequence hashCode

Introduction

In this page you can find the example usage for java.lang CharSequence hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:Strings.java

/**
 * Returns a hash code for a character sequence that is equivalent
 * to the hash code generated for a its string yield.  Recall that
 * the interface {@link CharSequence} does not refine the definition
 * of equality beyond that of {@link Object#equals(Object)}.
 *
 * <P>The return result is the same as would be produced by:
 *
 * <pre>/*www .j av a2  s. c  o m*/
 *    hashCode(cSeq) = cSeq.toString().hashCode()</pre>
 *
 * Recall that the {@link CharSequence} interface requires its
 * {@link CharSequence#toString()} to return a string
 * corresponding to its characters as returned by
 * <code>charAt(0),...,charAt(length()-1)</code>.  This value
 * can be defined directly by inspecting the hash code for strings:
 *
 * <pre>
 *      int h = 0;
 *      for (int i = 0; i < cSeq.length(); ++i)
 *          h = 31*h + cSeq.charAt(i);
 *      return h;</pre>
 *
 * @param cSeq The character sequence.
 * @return The hash code for the specified character sequence.
 */
public static int hashCode(CharSequence cSeq) {
    if (cSeq instanceof String)
        return cSeq.hashCode();
    int h = 0;
    for (int i = 0; i < cSeq.length(); ++i)
        h = 31 * h + cSeq.charAt(i);
    return h;
}

From source file:com.ntsync.android.sync.activities.MessageDialog.java

/**
 * Creates a Message Dialog and shows a Text-Message.
 * //from  w  w  w .j  a v a 2s  .  c o m
 * @param text
 * @param fragmentManager
 * 
 * @return visible Dialog
 */
public static MessageDialog show(CharSequence text, FragmentManager fragmentManager) {
    Bundle args = new Bundle();
    args.putCharSequence(PARAM_TEXT, text);
    return showDialog(args, fragmentManager, String.valueOf(text.hashCode()));
}

From source file:com.ntsync.android.sync.activities.MessageDialog.java

/**
 * Creates a Message Dialog and shows a Text-Message.
 * //w ww.j  a v a 2 s  .c om
 * @param resId
 * @param activity
 * 
 * @return visible Dialog
 */
public static MessageDialog showConfirmation(CharSequence msgText, CharSequence title,
        CharSequence positiveButtonText, FragmentActivity activity) {
    Bundle args = new Bundle();
    args.putCharSequence(PARAM_TEXT, msgText);
    args.putCharSequence(PARAM_TITLE, title);
    args.putCharSequence(PARAM_POSITIVEBUTTONTEXT, positiveButtonText);
    return showDialog(args, activity, String.valueOf(title.hashCode()));
}

From source file:com.github.nevo.decorators.reply.wechat.WeChatDecorator.java

private int calcSplitId(final CharSequence title) {
    return title == null ? 0 : title.hashCode();
}

From source file:com.eyc.statusBarNotification.StatusBarNotification.java

/**
 *    Displays status bar notification//from  w  w w  .  j  a v a  2s.  com
 *
 *    @param tag Notification tag.
 *  @param contentTitle   Notification title
 *  @param contentText   Notification text
 * */
public void showNotification(CharSequence tag, CharSequence contentTitle, CharSequence contentText, int flag) {
    String ns = Context.NOTIFICATION_SERVICE;
    context = cordova.getActivity().getApplicationContext();
    mNotificationManager = (NotificationManager) context.getSystemService(ns);

    Notification noti = StatusNotificationIntent.buildNotification(context, tag, contentTitle, contentText,
            flag);
    mNotificationManager.notify(tag.hashCode(), noti);
}

From source file:com.filemanager.free.activities.TextReader.java

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    if (charSequence.hashCode() == mInput.getText().hashCode()) {
        if (mTimer != null) {
            mTimer.cancel();/*from w ww .j a v a 2  s.co m*/
            mTimer.purge();
            mTimer = null;
        }
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mModified = !mInput.getText().toString().equals(mOriginal);
                invalidateOptionsMenu();
            }
        }, 250);
    }
}

From source file:com.filemanager.free.activities.TextReader.java

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    // clearing before adding new values
    if (searchEditText != null && charSequence.hashCode() == searchEditText.getText().hashCode()) {
        try {/*from   w  w w .  ja  va2s  .  c o m*/
            if (searchTextTask != null)
                searchTextTask.cancel(true);

            nodes.clear();
            mCurrent = -1;
            mLine = 0;

            Thread clearSpans = new Thread(this);
            clearSpans.run();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }
}

From source file:CharArrayMap.java

private int getHashCode(CharSequence text) {
    int code;/*from  ww w. jav  a  2  s  .co m*/
    if (ignoreCase) {
        code = 0;
        int len = text.length();
        for (int i = 0; i < len; i++) {
            code = code * 31 + Character.toLowerCase(text.charAt(i));
        }
    } else {
        if (false && text instanceof String) {
            code = text.hashCode();
        } else {
            code = 0;
            int len = text.length();
            for (int i = 0; i < len; i++) {
                code = code * 31 + text.charAt(i);
            }
        }
    }
    return code;
}

From source file:com.vadimfrolov.duorem.TargetConfigurationFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mHostBean = savedInstanceState.getParcelable(HostBean.EXTRA);
    }/*from w  ww  .  j a  v  a  2s .  co m*/

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_target_configuration, container, false);
    setHasOptionsMenu(true);

    mEditHostname = (TextInputEditText) v.findViewById(R.id.edit_hostname);
    mEditIpAddress = (TextInputEditText) v.findViewById(R.id.edit_ip_address);
    mEditBroadcastAddress = (TextInputEditText) v.findViewById(R.id.edit_broadcast_address);
    mViewBroadcastLayout = v.findViewById(R.id.input_layout_broadcast_address);
    mBtnGuessBroadcast = (Button) v.findViewById(R.id.btn_get_broadcast);
    mEditWolPort = (TextInputEditText) v.findViewById(R.id.edit_wol_port);
    mViewWolLayout = v.findViewById(R.id.input_layout_wol_port);
    mEditSshUsername = (TextInputEditText) v.findViewById(R.id.edit_ssh_username);
    mEditSshPassword = (TextInputEditText) v.findViewById(R.id.edit_ssh_password);
    mEditSshPort = (TextInputEditText) v.findViewById(R.id.edit_ssh_port);
    mSwitchAdvanced = (Switch) v.findViewById(R.id.switch_advanced);

    mEditShutdownCmd = (TextInputEditText) v.findViewById(R.id.edit_shutdown_cmd);
    mViewShutdownCmd = v.findViewById(R.id.input_layout_shutdown_cmd);

    EditText mac = (EditText) v.findViewById(R.id.mac_1);
    mEditMac.add(mac);
    mac = (EditText) v.findViewById(R.id.mac_2);
    mEditMac.add(mac);
    mac = (EditText) v.findViewById(R.id.mac_3);
    mEditMac.add(mac);
    mac = (EditText) v.findViewById(R.id.mac_4);
    mEditMac.add(mac);
    mac = (EditText) v.findViewById(R.id.mac_5);
    mEditMac.add(mac);
    mac = (EditText) v.findViewById(R.id.mac_6);
    mEditMac.add(mac);

    // This class does three things:
    // 1. Accepts full MAC address when user pastes it from the clipboard.
    // 2. Prevents entering of more than two characters in any of the MAC fields.
    // 3. Moves focus when user enters two characters in any of the MAC fields.
    TextWatcher macTextWatcher = new TextWatcher() {
        private boolean mIsPasting = false;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count == NetInfo.NOMAC.length() && s.toString().contains(":")) {
                String mac = s.toString();
                String[] parts = mac.split(":");
                if (parts.length != 6)
                    return;

                mIsPasting = true;
                for (int i = 0; i < 6; i++) {
                    mEditMac.get(i).setText(parts[i]);
                }
                mIsPasting = false;
                mEditBroadcastAddress.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mIsPasting)
                return;

            int len = s.toString().length();
            if (len == 2) {
                int target = -1;
                for (int i = 0; i < mEditMac.size(); i++) {
                    EditText candidate = mEditMac.get(i);
                    if (candidate.getText().hashCode() == s.hashCode()) {
                        target = i;
                        break;
                    }
                }
                if (target == -1)
                    return;

                if (target == 5) {
                    mEditBroadcastAddress.requestFocus();
                } else {
                    mEditMac.get(target + 1).requestFocus();
                }
            }

            if (len > 2) {
                s.delete(2, len);
            }
        }
    };

    for (EditText macEditor : mEditMac) {
        macEditor.addTextChangedListener(macTextWatcher);
    }

    mBtnGuessBroadcast.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NetInfo ni = new NetInfo(mContext);
            String ip = mEditIpAddress.getText().toString();
            String broadcastIp = NetInfo.getBroadcastFromIpAndCidr(ip, ni.cidr);
            mEditBroadcastAddress.setText(broadcastIp);
        }
    });

    mSwitchAdvanced.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setAdvancedVisibility(isChecked);
        }
    });

    return v;
}