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 * @since 0.17
14 */
15 public interface Violation extends Comparable<Violation> {
16
17 /**
18 * Name of the validator that generated this violation information.
19 * @return Name of the validator
20 */
21 String validator();
22
23 /**
24 * Name of the failed check.
25 * @return Name of the failed check
26 */
27 String name();
28
29 /**
30 * Validated file.
31 * @return Validated file
32 */
33 String file();
34
35 /**
36 * Lines with the problem.
37 * @return Lines with the problem
38 */
39 String lines();
40
41 /**
42 * Validation message.
43 * @return Validation message
44 */
45 String message();
46
47 /**
48 * Default validation result.
49 * @since 0.1
50 */
51 @EqualsAndHashCode
52 @ToString
53 final class Default implements Violation {
54
55 /**
56 * Total ordering across all observable fields, so that two
57 * violations only tie when they are fully equal.
58 */
59 private static final Comparator<Violation> ORDER =
60 Comparator.comparing(Violation::validator, String.CASE_INSENSITIVE_ORDER)
61 .thenComparing(Violation::file, String.CASE_INSENSITIVE_ORDER)
62 .thenComparing(Default::lineNumber)
63 .thenComparing(Violation::lines)
64 .thenComparing(Violation::name)
65 .thenComparing(Violation::message);
66
67 /**
68 * Name of the validator that generated this violation information.
69 */
70 private final String vldtr;
71
72 /**
73 * Name of the failed check.
74 */
75 private final String chk;
76
77 /**
78 * Lines with the problem.
79 */
80 private final String lns;
81
82 /**
83 * Validated file.
84 */
85 private final String path;
86
87 /**
88 * Validation message.
89 */
90 private final String msg;
91
92 /**
93 * Constructor.
94 * @param vldtr Name of the validator
95 * @param name Name of the failed check
96 * @param file Validated file
97 * @param lns Lines with the problem
98 * @param msg Validation message
99 * @checkstyle ParameterNumber (3 lines)
100 */
101 public Default(final String vldtr, final String name,
102 final String file, final String lns, final String msg) {
103 this.vldtr = vldtr;
104 this.chk = name;
105 this.path = file;
106 this.lns = lns;
107 this.msg = msg;
108 }
109
110 @Override
111 public String validator() {
112 return this.vldtr;
113 }
114
115 @Override
116 public String name() {
117 return this.chk;
118 }
119
120 @Override
121 public String file() {
122 return this.path;
123 }
124
125 @Override
126 public String lines() {
127 return this.lns;
128 }
129
130 @Override
131 public String message() {
132 return this.msg;
133 }
134
135 @Override
136 public int compareTo(final Violation other) {
137 return Default.ORDER.compare(this, other);
138 }
139
140 /**
141 * Numeric line number, or {@link Integer#MAX_VALUE} when the
142 * field cannot be parsed as a single integer (e.g. a range like
143 * {@code "10-12"}). Numeric ordering keeps line 9 before line 42.
144 * @param violation The violation to inspect
145 * @return Parsed line number, or {@code MAX_VALUE} as a fallback
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 }