summaryrefslogtreecommitdiff
path: root/hppc-template-processor/src/main/java/com/carrotsearch/hppc/generator/TemplateOptions.java
blob: 6ecf834a0eaf3f2b2dcf7f6d8ba7114bec59b186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.carrotsearch.hppc.generator;

import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.google.common.base.Preconditions;

/**
 * Template options for velocity directives in templates.
 */
public class TemplateOptions {
  public static final String TEMPLATE_FILE_TOKEN = "__TEMPLATE_SOURCE__";

  private boolean ignore;

  public Type ktype;
  public Type vtype;

  public Path templateFile;

  public TemplateOptions(Type ktype) {
    this(ktype, null);
  }

  public TemplateOptions(Type ktype, Type vtype) {
    this.ktype = ktype;
    this.vtype = vtype;
  }

  public void setIgnored(boolean ignore) {
    this.ignore = ignore;
  }

  public boolean isIgnored() {
    return ignore;
  }

  public boolean isKTypePrimitive() {
    return ktype != Type.GENERIC;
  }

  public boolean isVTypePrimitive() {
    return getVType() != Type.GENERIC;
  }

  public boolean isKTypeGeneric() {
    return ktype == Type.GENERIC;
  }

  public boolean isVTypeGeneric() {
    return getVType() == Type.GENERIC;
  }

  public boolean isAllGeneric() {
    return isKTypeGeneric() && isVTypeGeneric();
  }

  public boolean isAnyPrimitive() {
    return isKTypePrimitive() || isVTypePrimitive();
  }

  public boolean isAnyGeneric() {
    return isKTypeGeneric() || (hasVType() && isVTypeGeneric());
  }

  public boolean hasVType() {
    return vtype != null;
  }

  public boolean hasKType() {
    return true;
  }

  public Type getKType() {
    Preconditions.checkArgument(hasKType(), "Template does not specify KType.");
    return ktype;
  }

  public Type getVType() {
    Preconditions.checkArgument(hasVType(), "Template does not specify VType.");
    return vtype;
  }

  /*
   * Returns the current time in ISO format.
   */
  public String getTimeNow() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ROOT);
    return format.format(new Date());
  }

  public String getTemplateFile() {
    return templateFile.getFileName().toString();
  }

  public String getGeneratedAnnotation() {
    return String.format(Locale.ROOT, 
        "//@javax.annotation.Generated(\n" + 
        "//    date = \"%s\",\n" +
        "//    value = \"%s\")",
        getTimeNow(),
        TEMPLATE_FILE_TOKEN);
  }
}