Skip to content

Commit af6cb3e

Browse files
committed
Initial commit with code
1 parent fb4b32c commit af6cb3e

File tree

14 files changed

+2057
-0
lines changed

14 files changed

+2057
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Maven
2+
/target
3+
4+
*.class
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
/.classpath
11+
/.project
12+
/.settings/org.eclipse.core.resources.prefs
13+
/.settings/org.eclipse.jdt.core.prefs
14+
/.settings/org.eclipse.m2e.core.prefs
15+
/.settings/org.eclipse.jdt.ui.prefs
16+
/nbactions.xml
17+
/nbactions-ci.xml

pom.xml

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.sonatype.oss</groupId>
7+
<artifactId>oss-parent</artifactId>
8+
<version>9</version>
9+
</parent>
10+
11+
<groupId>pl.wavesoftware</groupId>
12+
<artifactId>eid-exceptions</artifactId>
13+
<version>0.1.0-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
16+
<name>EID Runtime Exceptions and Utilities</name>
17+
<description>
18+
This small library holds a set of Exceptions that implements idea of fast, reusable, error codes
19+
that can be simple thrown fast in case of unpredictable and unrecoverable application failure.
20+
</description>
21+
22+
<properties>
23+
<netbeans.hint.license>apache20</netbeans.hint.license>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
26+
<maven.compiler.source>1.6</maven.compiler.source>
27+
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
28+
</properties>
29+
30+
<licenses>
31+
<license>
32+
<name>Apache License 2.0</name>
33+
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
34+
<distribution>repo</distribution>
35+
</license>
36+
</licenses>
37+
38+
<scm>
39+
<connection>scm:git:https://github.com/wavesoftware/java-eid-exceptions.git</connection>
40+
<developerConnection>scm:git:[email protected]:wavesoftware/java-eid-exceptions.git</developerConnection>
41+
<url>https://github.com/wavesoftware/java-eid-exceptions</url>
42+
</scm>
43+
<ciManagement>
44+
<system>travis-ci</system>
45+
<url>https://travis-ci.org/wavesoftware/java-eid-exceptions</url>
46+
</ciManagement>
47+
48+
<dependencies>
49+
50+
<!-- Tests -->
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.12</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.assertj</groupId>
59+
<artifactId>assertj-core</artifactId>
60+
<version>2.1.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.code.findbugs</groupId>
65+
<artifactId>jsr305</artifactId>
66+
<version>3.0.0</version>
67+
<type>jar</type>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.hamcrest</groupId>
71+
<artifactId>hamcrest-all</artifactId>
72+
<version>1.3</version>
73+
<scope>test</scope>
74+
<type>jar</type>
75+
</dependency>
76+
77+
</dependencies>
78+
79+
<profiles>
80+
<profile>
81+
<id>ci</id>
82+
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.jacoco</groupId>
87+
<artifactId>jacoco-maven-plugin</artifactId>
88+
<version>0.7.4.201502262128</version>
89+
90+
<executions>
91+
<execution>
92+
<id>jacoco-initialize</id>
93+
<goals>
94+
<goal>prepare-agent</goal>
95+
<goal>prepare-agent-integration</goal>
96+
</goals>
97+
</execution>
98+
<execution>
99+
<id>jacoco-site</id>
100+
<phase>verify</phase>
101+
<goals>
102+
<goal>report</goal>
103+
<goal>report-integration</goal>
104+
</goals>
105+
</execution>
106+
</executions>
107+
<configuration>
108+
<includes>
109+
<include>pl/wavesoftware/**</include>
110+
</includes>
111+
</configuration>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</profile>
116+
117+
<profile>
118+
<id>travis</id>
119+
<activation>
120+
<property>
121+
<name>env.TRAVIS</name>
122+
<value>true</value>
123+
</property>
124+
</activation>
125+
<build>
126+
<plugins>
127+
<plugin>
128+
<groupId>org.eluder.coveralls</groupId>
129+
<artifactId>coveralls-maven-plugin</artifactId>
130+
<version>3.1.0</version>
131+
<executions>
132+
<execution>
133+
<id>coveralls-default</id>
134+
<phase>verify</phase>
135+
<goals>
136+
<goal>report</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
</plugin>
141+
</plugins>
142+
</build>
143+
</profile>
144+
</profiles>
145+
146+
<build>
147+
<plugins>
148+
149+
<plugin>
150+
<groupId>org.apache.maven.plugins</groupId>
151+
<artifactId>maven-compiler-plugin</artifactId>
152+
<version>3.3</version>
153+
<configuration>
154+
<compilerArgs>
155+
<arg>-Xlint</arg>
156+
</compilerArgs>
157+
</configuration>
158+
</plugin>
159+
160+
<plugin>
161+
<groupId>org.apache.maven.plugins</groupId>
162+
<artifactId>maven-jar-plugin</artifactId>
163+
<version>2.6</version>
164+
<configuration>
165+
<useDefaultManifestFile>true</useDefaultManifestFile>
166+
</configuration>
167+
</plugin>
168+
169+
<plugin>
170+
<groupId>org.apache.maven.plugins</groupId>
171+
<artifactId>maven-surefire-plugin</artifactId>
172+
<version>2.18.1</version>
173+
<configuration>
174+
<trimStackTrace>false</trimStackTrace>
175+
</configuration>
176+
</plugin>
177+
178+
<plugin>
179+
<groupId>org.apache.maven.plugins</groupId>
180+
<artifactId>maven-failsafe-plugin</artifactId>
181+
<version>2.18.1</version>
182+
<configuration>
183+
<trimStackTrace>false</trimStackTrace>
184+
<encoding>UTF-8</encoding>
185+
</configuration>
186+
<executions>
187+
<execution>
188+
<goals>
189+
<goal>integration-test</goal>
190+
<goal>verify</goal>
191+
</goals>
192+
</execution>
193+
</executions>
194+
</plugin>
195+
</plugins>
196+
</build>
197+
198+
</project>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2015 Krzysztof Suszyński <[email protected]>.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package pl.wavesoftware.eid.exceptions;
17+
18+
import java.io.Serializable;
19+
import static java.lang.Math.abs;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.Random;
23+
import javax.annotation.Nullable;
24+
25+
/**
26+
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
27+
* of end user applications which will report Bugs in standarized error pages or post them to issue tracker.
28+
* <p>
29+
* Exception identifier for all Eid Runtime Exceptions.
30+
*
31+
* @author Krzysztof Suszyński <[email protected]>
32+
*/
33+
public class Eid implements Serializable {
34+
35+
public static final String DEFAULT_FORMAT = "[%s]<%s>";
36+
37+
public static final String DEFAULT_REF_FORMAT = "[%s|%s]<%s>";
38+
39+
private static final long serialVersionUID = -9876432123423401L;
40+
41+
private static final Random RANDOM = new Random(System.currentTimeMillis());
42+
43+
private static String format = DEFAULT_FORMAT;
44+
45+
private static String refFormat = DEFAULT_REF_FORMAT;
46+
47+
private final String id;
48+
49+
private final String ref;
50+
51+
private final String uniq;
52+
53+
/**
54+
* Sets the actual format that will be used in {@link toString()} method. It will return previously used format.
55+
*
56+
* @param format a format compliant with {@link String#format(String, Object...)} with 2 object arguments
57+
* @return a previously used format
58+
* @throws NullPointerException if given format was null
59+
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>
60+
*/
61+
public static String setFormat(String format) throws NullPointerException, IllegalArgumentException {
62+
validateFormat(format, 2);
63+
String prevoiusly = Eid.format;
64+
Eid.format = format;
65+
return prevoiusly;
66+
}
67+
68+
/**
69+
* Sets the actual format that will be used in {@link toString()} method
70+
*
71+
* @param refFormat a format compliant with {@link String#format(String, Object...)} with 3 object arguments
72+
* @return a previously used format
73+
* @throws NullPointerException if given format was null
74+
* @throws IllegalArgumentException if given format hasn't got tree format specifiers <tt>"%s"</tt>
75+
*/
76+
public static String setRefFormat(String refFormat) {
77+
validateFormat(refFormat, 3);
78+
String prevoiusly = Eid.refFormat;
79+
Eid.refFormat = refFormat;
80+
return prevoiusly;
81+
}
82+
83+
/**
84+
* Constructor
85+
*
86+
* @param id the exception id, must be unique developer insereted string, from date
87+
* @param ref an optional reference
88+
*/
89+
public Eid(String id, @Nullable String ref) {
90+
uniq = Integer.toString(abs(Long.valueOf(abs(RANDOM.nextLong()) + abs(RANDOM.nextInt())).intValue()), 36);
91+
this.id = id;
92+
this.ref = ref == null ? "" : ref;
93+
}
94+
95+
/**
96+
* Constructor
97+
*
98+
* @param id the exception id, must be unique developer insereted string, from date
99+
*/
100+
public Eid(String id) {
101+
this(id, null);
102+
}
103+
104+
@Override
105+
public String toString() {
106+
if ("".equals(ref)) {
107+
return String.format(format, id, uniq);
108+
}
109+
return String.format(refFormat, id, ref, uniq);
110+
}
111+
112+
/**
113+
* Getter for constant Exception ID
114+
*
115+
* @return ID of exception
116+
*/
117+
public String getId() {
118+
return id;
119+
}
120+
121+
/**
122+
* Get custom ref passed to Exception ID
123+
*
124+
* @return ID of exception
125+
*/
126+
public String getRef() {
127+
return ref;
128+
}
129+
130+
/**
131+
* Gets uniq generated string for this instance of Eid
132+
*
133+
* @return a uniq string
134+
*/
135+
public String getUniq() {
136+
return uniq;
137+
}
138+
139+
static void validateFormat(String format, int numSpecifiers) throws NullPointerException, IllegalArgumentException {
140+
if (format == null) {
141+
throw new NullPointerException("Format can't be null, but just recieved one");
142+
}
143+
List<String> specifiers = new ArrayList<String>();
144+
for (int i = 0; i < numSpecifiers; i++) {
145+
specifiers.add(i + Integer.toString(abs(RANDOM.nextInt()), 36));
146+
}
147+
String formated = String.format(format, specifiers.toArray());
148+
for (String specifier : specifiers) {
149+
if (!formated.contains(specifier)) {
150+
throw new IllegalArgumentException("Given format contains to little format specifiers, "
151+
+ "expected " + numSpecifiers + " but given \"" + format + "\"");
152+
}
153+
}
154+
}
155+
156+
}

0 commit comments

Comments
 (0)