HashCode 使用产生Utils类

2024-08-21 00:38
文章标签 使用 产生 hashcode utils

本文主要是介绍HashCode 使用产生Utils类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

hashCode 和equals 这两个方法一直是使用的焦点!我们在使用比较对象的使用很多情况下都要使用这个,随便使用将造成很多困扰,详情见-effectjava
  1. 无意中发现一个写的还不错的utils 类,专门为了这个二创造的。
    以17为底数,37为乘
package org.apache.commons.httpclient.util;public class LangUtils {public static final int HASH_SEED = 17;public static final int HASH_OFFSET = 37;private LangUtils() {super();}public static int hashCode(final int seed, final int hashcode) {return seed * HASH_OFFSET + hashcode;}public static int hashCode(final int seed, final Object obj) {return hashCode(seed, obj != null ? obj.hashCode() : 0);}public static int hashCode(final int seed, final boolean b) {return hashCode(seed, b ? 1 : 0);}public static boolean equals(final Object obj1, final Object obj2) {return obj1 == null ? obj2 == null : obj1.equals(obj2);}}
  1. httpPost这个类很好的说明在使用equlas 和 hashcode
/** $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpHost.java,v 1.3 2005/01/14 21:16:40 olegk Exp $* $Revision: 510587 $* $Date: 2007-02-22 17:56:08 +0100 (Thu, 22 Feb 2007) $** ====================================================================**  Licensed to the Apache Software Foundation (ASF) under one or more*  contributor license agreements.  See the NOTICE file distributed with*  this work for additional information regarding copyright ownership.*  The ASF licenses this file to You under the Apache License, Version 2.0*  (the "License"); you may not use this file except in compliance with*  the License.  You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0**  Unless required by applicable law or agreed to in writing, software*  distributed under the License is distributed on an "AS IS" BASIS,*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*  See the License for the specific language governing permissions and*  limitations under the License.* ====================================================================** This software consists of voluntary contributions made by many* individuals on behalf of the Apache Software Foundation.  For more* information on the Apache Software Foundation, please see* <http://www.apache.org/>.**/package org.apache.commons.httpclient;import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.util.LangUtils;/*** Holds all of the variables needed to describe an HTTP connection to a host. This includes * remote host, port and protocol.* * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>* @author Laura Werner* * @since 3.0 */
public class HttpHost implements Cloneable {/** The host to use. */private String hostname = null;/** The port to use. */private int port = -1;/** The protocol */private Protocol protocol = null;/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.* @param port the port. Value <code>-1</code> can be used to set default protocol port* @param protocol the protocol. Value <code>null</code> can be used to set default protocol*/public HttpHost(final String hostname, int port, final Protocol protocol) {super();if (hostname == null) {throw new IllegalArgumentException("Host name may not be null");}if (protocol == null) {throw new IllegalArgumentException("Protocol may not be null");}this.hostname = hostname;this.protocol = protocol;if (port >= 0) {this.port = port;} else {this.port = this.protocol.getDefaultPort();}}/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.* @param port the port. Value <code>-1</code> can be used to set default protocol port*/public HttpHost(final String hostname, int port) {this(hostname, port, Protocol.getProtocol("http"));}/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.*/public HttpHost(final String hostname) {this(hostname, -1, Protocol.getProtocol("http"));}/*** URI constructor for HttpHost.*   * @param uri the URI.*/public  HttpHost(final URI uri) throws URIException {this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));}/*** Copy constructor for HttpHost* * @param httphost the HTTP host to copy details from*/public HttpHost (final HttpHost httphost) {super();init(httphost);}private void init(final HttpHost httphost) {this.hostname = httphost.hostname;this.port = httphost.port;this.protocol = httphost.protocol;}/*** @throws CloneNotSupportedException * @see java.lang.Object#clone()*/public Object clone() throws CloneNotSupportedException {HttpHost copy = (HttpHost) super.clone();copy.init(this);return copy;}    /*** Returns the host name (IP or DNS name).* * @return the host name (IP or DNS name), or <code>null</code> if not set*/public String getHostName() {return this.hostname;}/*** Returns the port.* * @return the host port, or <code>-1</code> if not set*/public int getPort() {return this.port;}/*** Returns the protocol.* @return The protocol.*/public Protocol getProtocol() {return this.protocol;}/*** Return the host uri.* * @return The host uri.*/public String toURI() {StringBuffer buffer = new StringBuffer(50);        buffer.append(this.protocol.getScheme());buffer.append("://");buffer.append(this.hostname);if (this.port != this.protocol.getDefaultPort()) {buffer.append(':');buffer.append(this.port);}return buffer.toString();}/*** @see java.lang.Object#toString()*/public String toString() {StringBuffer buffer = new StringBuffer(50);        buffer.append(toURI());return buffer.toString();}    /*** @see java.lang.Object#equals(java.lang.Object)*/public boolean equals(final Object o) {if (o instanceof HttpHost) {// shortcut if we're comparing with ourselvesif (o == this) { return true;}HttpHost that = (HttpHost) o;if (!this.hostname.equalsIgnoreCase(that.hostname)) {return false;}if (this.port != that.port) {return false;}if (!this.protocol.equals(that.protocol)) {return false;}// everything matchesreturn true;} else {return false;}}/*** @see java.lang.Object#hashCode()*/public int hashCode() {int hash = LangUtils.HASH_SEED;hash = LangUtils.hashCode(hash, this.hostname);hash = LangUtils.hashCode(hash, this.port);hash = LangUtils.hashCode(hash, this.protocol);return hash;}}

这篇关于HashCode 使用产生Utils类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091625

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数