当前位置: 首页 > news >正文

成都企业网站建设价格黄金网站app视频下载小说

成都企业网站建设价格,黄金网站app视频下载小说,南京新标特企业网站哪家广告做的,企业网站的意思在回答我最近的文章中AutoValue#xff1a;生成的不可变的值类 #xff0c; 布兰登认为#xff0c;这可能是有趣的#xff0c;看看如何AutoValue比较项目Lombok和Immutables和凯文借调这一点。 我同意这是一个好主意#xff0c;但是我首先将这篇文章发布为Immutables的简要… 在回答我最近的文章中AutoValue生成的不可变的值类 布兰登认为这可能是有趣的看看如何AutoValue比较项目Lombok和Immutables和凯文借调这一点。 我同意这是一个好主意但是我首先将这篇文章发布为Immutables的简要概述因为我已经为Lombok和AutoValue提供了类似的文章。 可从Maven中央存储库中获得Immutables 2.2.5 其许可证页面指出“ Imputables工具箱和所有必需的依赖项均在Apache软件许可2.0版中涵盖。” 开始吧 网页指出“运行Immutables注释处理器需要Java 7或更高版本。” 像AutoValue这样的不可变对象使用编译时注释来生成定义不可变对象的类的源代码。 因为它们都使用这种方法所以都只引入了编译时依赖性并且在应用程序的运行时类路径上不需要它们各自的JAR。 换句话说不可变的JAR必须位于编译器 javac 的类路径上而不是位于Java启动器 java 的类路径上。 下一个代码清单 Person.java 中显示了“模板” Person类的代码清单。 它看起来与我在AutoValue演示中使用的Person.java非常相似。 人.java package dustin.examples.immutables;import org.immutables.value.Value;/*** Represents an individual as part of demonstration of* the Immutables project (http://immutables.github.io/).*/ Value.Immutable // concrete extension will be generated by Immutables abstract class Person {/*** Provide Persons last name.** return Last name of person.*/abstract String lastName();/*** Provide Persons first name.** return First name of person.*/abstract String firstName();/*** Provide Persons birth year.** return Persons birth year.*/abstract long birthYear(); } 我在AutoValue帖子中列出的“ template”类和“ template”类的唯一区别是包的名称正在演示哪个产品的Javadoc注释以及最重要的是导入并应用于类。 在AutoValue示例中有一个特定的“创建”方法不在Immutables示例中但这只是因为我没有演示AutoValue生成器的使用这将使“ create”方法变得不必要。 当我在类路径上适当地指定使用Immutables并使用javac编译以上源代码时将调用注释处理器并生成以下Java源代码 ImmutablePerson.java package dustin.examples.immutables;import java.util.ArrayList; import java.util.List; import java.util.Objects; import javax.annotation.Generated;/*** Immutable implementation of {link Person}.* p* Use the builder to create immutable instances:* {code ImmutablePerson.builder()}.*/ SuppressWarnings(all) Generated({Immutables.generator, Person}) final class ImmutablePerson extends Person {private final String lastName;private final String firstName;private final long birthYear;private ImmutablePerson(String lastName, String firstName, long birthYear) {this.lastName lastName;this.firstName firstName;this.birthYear birthYear;}/*** return The value of the {code lastName} attribute*/OverrideString lastName() {return lastName;}/*** return The value of the {code firstName} attribute*/OverrideString firstName() {return firstName;}/*** return The value of the {code birthYear} attribute*/Overridelong birthYear() {return birthYear;}/*** Copy the current immutable object by setting a value for the {link Person#lastName() lastName} attribute.* An equals check used to prevent copying of the same value by returning {code this}.* param lastName A new value for lastName* return A modified copy of the {code this} object*/public final ImmutablePerson withLastName(String lastName) {if (this.lastName.equals(lastName)) return this;String newValue Objects.requireNonNull(lastName, lastName);return new ImmutablePerson(newValue, this.firstName, this.birthYear);}/*** Copy the current immutable object by setting a value for the {link Person#firstName() firstName} attribute.* An equals check used to prevent copying of the same value by returning {code this}.* param firstName A new value for firstName* return A modified copy of the {code this} object*/public final ImmutablePerson withFirstName(String firstName) {if (this.firstName.equals(firstName)) return this;String newValue Objects.requireNonNull(firstName, firstName);return new ImmutablePerson(this.lastName, newValue, this.birthYear);}/*** Copy the current immutable object by setting a value for the {link Person#birthYear() birthYear} attribute.* A value equality check is used to prevent copying of the same value by returning {code this}.* param birthYear A new value for birthYear* return A modified copy of the {code this} object*/public final ImmutablePerson withBirthYear(long birthYear) {if (this.birthYear birthYear) return this;return new ImmutablePerson(this.lastName, this.firstName, birthYear);}/*** This instance is equal to all instances of {code ImmutablePerson} that have equal attribute values.* return {code true} if {code this} is equal to {code another} instance*/Overridepublic boolean equals(Object another) {if (this another) return true;return another instanceof ImmutablePerson equalTo((ImmutablePerson) another);}private boolean equalTo(ImmutablePerson another) {return lastName.equals(another.lastName) firstName.equals(another.firstName) birthYear another.birthYear;}/*** Computes a hash code from attributes: {code lastName}, {code firstName}, {code birthYear}.* return hashCode value*/Overridepublic int hashCode() {int h 31;h h * 17 lastName.hashCode();h h * 17 firstName.hashCode();h h * 17 Long.hashCode(birthYear);return h;}/*** Prints the immutable value {code Person} with attribute values.* return A string representation of the value*/Overridepublic String toString() {return Person{ lastName lastName , firstName firstName , birthYear birthYear };}/*** Creates an immutable copy of a {link Person} value.* Uses accessors to get values to initialize the new immutable instance.* If an instance is already immutable, it is returned as is.* param instance The instance to copy* return A copied immutable Person instance*/public static ImmutablePerson copyOf(Person instance) {if (instance instanceof ImmutablePerson) {return (ImmutablePerson) instance;}return ImmutablePerson.builder().from(instance).build();}/*** Creates a builder for {link ImmutablePerson ImmutablePerson}.* return A new ImmutablePerson builder*/public static ImmutablePerson.Builder builder() {return new ImmutablePerson.Builder();}/*** Builds instances of type {link ImmutablePerson ImmutablePerson}.* Initialize attributes and then invoke the {link #build()} method to create an* immutable instance.* pem{code Builder} is not thread-safe and generally should not be stored in a field or collection,* but instead used immediately to create instances./em*/static final class Builder {private static final long INIT_BIT_LAST_NAME 0x1L;private static final long INIT_BIT_FIRST_NAME 0x2L;private static final long INIT_BIT_BIRTH_YEAR 0x4L;private long initBits 0x7L;private String lastName;private String firstName;private long birthYear;private Builder() {}/*** Fill a builder with attribute values from the provided {code Person} instance.* Regular attribute values will be replaced with those from the given instance.* Absent optional values will not replace present values.* param instance The instance from which to copy values* return {code this} builder for use in a chained invocation*/public final Builder from(Person instance) {Objects.requireNonNull(instance, instance);lastName(instance.lastName());firstName(instance.firstName());birthYear(instance.birthYear());return this;}/*** Initializes the value for the {link Person#lastName() lastName} attribute.* param lastName The value for lastName * return {code this} builder for use in a chained invocation*/public final Builder lastName(String lastName) {this.lastName Objects.requireNonNull(lastName, lastName);initBits ~INIT_BIT_LAST_NAME;return this;}/*** Initializes the value for the {link Person#firstName() firstName} attribute.* param firstName The value for firstName * return {code this} builder for use in a chained invocation*/public final Builder firstName(String firstName) {this.firstName Objects.requireNonNull(firstName, firstName);initBits ~INIT_BIT_FIRST_NAME;return this;}/*** Initializes the value for the {link Person#birthYear() birthYear} attribute.* param birthYear The value for birthYear * return {code this} builder for use in a chained invocation*/public final Builder birthYear(long birthYear) {this.birthYear birthYear;initBits ~INIT_BIT_BIRTH_YEAR;return this;}/*** Builds a new {link ImmutablePerson ImmutablePerson}.* return An immutable instance of Person* throws java.lang.IllegalStateException if any required attributes are missing*/public ImmutablePerson build() {if (initBits ! 0) {throw new IllegalStateException(formatRequiredAttributesMessage());}return new ImmutablePerson(lastName, firstName, birthYear);}private String formatRequiredAttributesMessage() {ListString attributes new ArrayListString();if ((initBits INIT_BIT_LAST_NAME) ! 0) attributes.add(lastName);if ((initBits INIT_BIT_FIRST_NAME) ! 0) attributes.add(firstName);if ((initBits INIT_BIT_BIRTH_YEAR) ! 0) attributes.add(birthYear);return Cannot build Person, some of required attributes are not set attributes;}} } 通过检查生成的代码可以得出一些结论您会发现它们与我之前的文章中为AutoValue列出的观察结果非常相似 生成的类扩展实现继承了手写的抽象类从而允许使用代码使用手写类的API而不必知道正在使用生成的类。 即使没有在源类中直接定义任何字段也将生成字段 不可变对象根据提供的abstract访问器方法解释字段。 生成的类不为字段提供“设置” / mutator方法get / accessor方法。 这并不奇怪因为值对象的关键概念是它们是不可变的甚至这个项目的名称 Immutables 也暗示了这一特征。 请注意 Immutables确实为具有 Value.Modifiable批注的可修改对象提供了一些功能。 考虑到每个字段的类型将自动为每个字段适当地生成equalsObject hashCode和toString的实现。 在源类和方法上的Javadoc注释不会在生成的扩展类上重现。 相反在生成的类的方法上提供了更简单更通用的Javadoc注释在构建器类的方法上提供了更重要但仍是通用的的Javadoc注释。 正如我关于AutoValue所述使用诸如Immutables生成之类的方法的主要优点之一是开发人员可以专注于特定类应支持的更高级的概念而代码生成则可以确保较低级别的细节。一致且正确地实施。 但是使用这种方法时要记住一些事情。 当开发人员受过足够的训练以检查和维护抽象的“源” Java类而不是生成的类时 不可变对象最有帮助。 下次注释处理再次生成该类时对生成类的更改将被覆盖否则必须停止该类的生成以免发生这种情况。 您将需要设置build / IDE以便将生成的类视为“源代码”以便抽象类可以编译并且对生成的类的任何依赖项也可以编译。 如果要将可变字段与不可变项一起使用则必须特别小心如果通常要选择使用不可变项或值对象通常就是这种情况。 结论 我的结论几乎与我在AutoValue上发表的文章一样。 Immutables允许开发人员编写更简洁的代码重点放在高级细节上并将繁琐的底层通常是容易出错的细节委派给Immutables 以实现自动代码生成。 这类似于IDE的源代码生成可以执行的操作但是Immutables相对于IDE方法的优势是Immutables可以在每次编译代码时重新生成源代码从而使生成的代码保持最新。 Immutables的这一优势也是Java自定义注释处理功能强大的一个很好的例子。 翻译自: https://www.javacodegeeks.com/2016/06/creating-value-objects-immutables.html
http://www.pierceye.com/news/371338/

相关文章:

  • 海尔网站建设内容策划wordpress 登录密码
  • 金融公司网站规划方案四川省住建厅特种作业证报名
  • 做网站员培训网站小视频怎么做
  • 做网站是学什么专业的电子商务网络营销方式
  • 东莞电商网站公司goz建站
  • 深圳石岩建网站权威发布李建
  • 大连哪家公司做网站比较好网页搜索的快捷键
  • 怎样建个小公司的网站濮阳网络电视直播
  • 台州低价网站建设阆中做网站
  • 兰州网站运营诊断学校网站报价方案
  • 宿迁做网站大公司现在企业做网站一般用什么框架
  • 企业如何建自己的网站自己网站的登录api怎么做
  • 专业的网站建设企业微信小程序服务器一年多少钱
  • 关于网站建设的句子苏州实力做网站公司有哪些
  • 网页制作与网站建设》在线作业 答案wordpress信息量几百万
  • 代刷网站系统怎么做wordpress数据库连接
  • 邢台网站改版开发开封美食网站建设规划
  • 网站建设佰金手指科杰二五国内网站推广
  • wordpress 多站点 用户天津经济持续恢复
  • 做网站邯郸怎样建立平台
  • 网站中捕获鼠标位置mip wordpress 评论
  • 室内设计资料网站discuz是什么东西
  • 网站建设方向论文提纲网页作品制作的一般过程
  • 生道网站建设平台高端商品网站
  • 网站维护计划商标注册号查询入口官网
  • 个人怎样申请网站网站整站html
  • 网站规则购房网官网
  • 苏州市吴江区住房和城乡建设局网站西安市今天发生的重大新闻
  • 网站需求建设关系书赚钱软件学生
  • 长春做网站4435怎么做手机网站