View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.spi;
6   
7   import java.util.Comparator;
8   import lombok.EqualsAndHashCode;
9   import lombok.ToString;
10  
11  /**
12   * Validation result.
13   *
14   * @since 0.17
15   */
16  public interface Violation extends Comparable<Violation> {
17  
18      /**
19       * Name of the validator that generated this violation information.
20       *
21       * @return Name of the validator
22       */
23      String validator();
24  
25      /**
26       * Name of the failed check.
27       *
28       * @return Name of the failed check
29       */
30      String name();
31  
32      /**
33       * Validated file.
34       *
35       * @return Validated file
36       */
37      String file();
38  
39      /**
40       * Lines with the problem.
41       *
42       * @return Lines with the problem
43       */
44      String lines();
45  
46      /**
47       * Validation message.
48       *
49       * @return Validation message
50       */
51      String message();
52  
53      /**
54       * Default validation result.
55       *
56       * @since 0.1
57       */
58      @EqualsAndHashCode
59      @ToString
60      final class Default implements Violation {
61  
62          /**
63           * Total ordering across all observable fields, so that two
64           * violations only tie when they are fully equal.
65           */
66          private static final Comparator<Violation> ORDER =
67              Comparator.comparing(Violation::validator, String.CASE_INSENSITIVE_ORDER)
68                  .thenComparing(Violation::file, String.CASE_INSENSITIVE_ORDER)
69                  .thenComparingInt(Default::lineNumber)
70                  .thenComparing(Violation::lines)
71                  .thenComparing(Violation::name)
72                  .thenComparing(Violation::message);
73  
74          /**
75           * Name of the validator that generated this violation information.
76           */
77          private final String vldtr;
78  
79          /**
80           * Name of the failed check.
81           */
82          private final String chk;
83  
84          /**
85           * Lines with the problem.
86           */
87          private final String lns;
88  
89          /**
90           * Validated file.
91           */
92          private final String path;
93  
94          /**
95           * Validation message.
96           */
97          private final String msg;
98  
99          /**
100          * Constructor.
101          *
102          * @param vldtr Name of the validator
103          * @param name Name of the failed check
104          * @param file Validated file
105          * @param lns Lines with the problem
106          * @param msg Validation message
107          */
108         public Default(final String vldtr, final String name,
109             final String file, final String lns, final String msg) {
110             this.vldtr = vldtr;
111             this.chk = name;
112             this.path = file;
113             this.lns = lns;
114             this.msg = msg;
115         }
116 
117         @Override
118         public String validator() {
119             return this.vldtr;
120         }
121 
122         @Override
123         public String name() {
124             return this.chk;
125         }
126 
127         @Override
128         public String file() {
129             return this.path;
130         }
131 
132         @Override
133         public String lines() {
134             return this.lns;
135         }
136 
137         @Override
138         public String message() {
139             return this.msg;
140         }
141 
142         @Override
143         public int compareTo(final Violation other) {
144             return Default.ORDER.compare(this, other);
145         }
146 
147         private static int lineNumber(final Violation violation) {
148             int parsed;
149             try {
150                 parsed = Integer.parseInt(violation.lines());
151             } catch (final NumberFormatException ignored) {
152                 parsed = Integer.MAX_VALUE;
153             }
154             return parsed;
155         }
156     }
157 }