jclouds-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zack Shoylev <notificati...@github.com>
Subject Re: [jclouds-labs-openstack] Poppy service api (#179)
Date Mon, 09 Mar 2015 23:32:55 GMT
> +import org.jclouds.json.SerializedNames;
> +
> +import com.google.auto.value.AutoValue;
> +
> +/**
> + * Representation of an OpenStack Poppy Caching Rule.
> + */
> +@AutoValue
> +public abstract class Caching {
> +   public abstract String getName();
> +   public abstract int getTtl();
> +   @Nullable public abstract List<CachingRule> getRules();
> +
> +   @SerializedNames({ "name", "ttl", "rules" })
> +   private static Caching create(String name, int ttl, List<CachingRule> rules)
{
> +      return builder().name(name).ttl(ttl).rules(rules).build();

@nacx I am moving the discussion here from IRC.
It's true, using copyOf will ensure that the deserialized object's rules List<CachingRule>
getRules() list is immutable.

However, consider this:
Caching caching = Caching.builder().rules(new List()).build();
caching.getRules().add(CachingRule.builder().build());

The generated Builder types have to be the same as the autovalue getter types. Also, I don't
think you can change the behavior of the builder setters.

This is the generated code:

final class AutoValue_Caching extends Caching {

  private final String name;
  private final int ttl;
  private final List<CachingRule> rules;

  private AutoValue_Caching(
      String name,
      int ttl,
      List<CachingRule> rules) {
    if (name == null) {
      throw new NullPointerException("Null name");
    }
    this.name = name;
    this.ttl = ttl;
    this.rules = rules;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public int getTtl() {
    return ttl;
  }

  @org.jclouds.javax.annotation.Nullable
  @Override
  public List<CachingRule> getRules() {
    return rules;
  }
  
...

  @Override
  public Caching.Builder toBuilder() {
    return new Builder(this);
  }

  static final class Builder implements Caching.Builder {
    private String name;
    private Integer ttl;
    private List<CachingRule> rules;
    Builder() {
    }
    Builder(Caching source) {
      name(source.getName());
      ttl(source.getTtl());
      rules(source.getRules());
    }
    @Override
    public Caching.Builder name(String name) {
      this.name = name;
      return this;
    }
    @Override
    public Caching.Builder ttl(int ttl) {
      this.ttl = ttl;
      return this;
    }
    @Override
    public Caching.Builder rules(List<CachingRule> rules) {
      this.rules = rules;
      return this;
    }
    @Override
    public Caching build() {
      String missing = "";
      if (name == null) {
        missing += " name";
      }
      if (ttl == null) {
        missing += " ttl";
      }
      if (!missing.isEmpty()) {
        throw new IllegalStateException("Missing required properties:" + missing);
      }
      Caching result = new AutoValue_Caching(
          this.name,
          this.ttl,
          this.rules);
      return result;
    }
  }
}

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-labs-openstack/pull/179/files#r26087263
Mime
View raw message