Skip to content

Commit

Permalink
#164 - Introduce checkstyle
Browse files Browse the repository at this point in the history
- Auto-format dkpro-jwpl-wikimachine
  • Loading branch information
reckart committed Oct 31, 2023
1 parent d3587d6 commit 1b9a679
Show file tree
Hide file tree
Showing 42 changed files with 2,919 additions and 2,513 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,55 @@
package org.dkpro.jwpl.wikimachine.debug;

/**
* Logger, which does not implement some concrete output technique, but knows
* how exceptions are handled. AbstractLogger provides a template
* method {@link #log(Object)} for its derivatives.
* Logger, which does not implement some concrete output technique, but knows how exceptions are
* handled. AbstractLogger provides a template method {@link #log(Object)} for its derivatives.
*/
public abstract class AbstractLogger implements ILogger {
public abstract class AbstractLogger
implements ILogger
{

protected boolean isThrowable(Class<?> c) {
boolean throwable = false;
if (c != null) {
throwable = c.equals(Throwable.class);
if (!throwable) {
for (Class<?> i : c.getInterfaces()) {
if (throwable |= isThrowable(i)) {
break;
}
protected boolean isThrowable(Class<?> c)
{
boolean throwable = false;
if (c != null) {
throwable = c.equals(Throwable.class);
if (!throwable) {
for (Class<?> i : c.getInterfaces()) {
if (throwable |= isThrowable(i)) {
break;
}
}
if (!throwable) {
throwable |= isThrowable(c.getSuperclass());
}
}
}
if (!throwable) {
throwable |= isThrowable(c.getSuperclass());
}
}
return throwable;
}
return throwable;
}

protected String createThrowableMessage(Throwable e) {
StringBuilder message = new StringBuilder();
message.append(e.getMessage());
message.append('\n');
for (StackTraceElement currentTrace : e.getStackTrace()) {
message.append('\n');
message.append(currentTrace);
protected String createThrowableMessage(Throwable e)
{
StringBuilder message = new StringBuilder();
message.append(e.getMessage());
message.append('\n');
for (StackTraceElement currentTrace : e.getStackTrace()) {
message.append('\n');
message.append(currentTrace);
}
return message.toString();
}
return message.toString();
}

@Override
public void log(Object message) {
if (isThrowable(message.getClass())) {
logObject(createThrowableMessage((Throwable) message));
} else {
logObject(message);
@Override
public void log(Object message)
{
if (isThrowable(message.getClass())) {
logObject(createThrowableMessage((Throwable) message));
}
else {
logObject(message);
}
}
}

protected abstract void logObject(Object message);
protected abstract void logObject(Object message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@
*/
package org.dkpro.jwpl.wikimachine.debug;


/**
* A logger implementation which combines several {@link ILogger} instances into a chain which is processed in
* the order in which the chain was initially composed.
* A logger implementation which combines several {@link ILogger} instances into a chain which is
* processed in the order in which the chain was initially composed.
*/
public class CompositeLogger implements ILogger {
public class CompositeLogger
implements ILogger
{

private final ILogger[] loggers;
private final ILogger[] loggers;

public CompositeLogger(ILogger[] initLoggers) {
loggers = (initLoggers != null) ? initLoggers : new ILogger[0];
}
public CompositeLogger(ILogger[] initLoggers)
{
loggers = (initLoggers != null) ? initLoggers : new ILogger[0];
}

@Override
public void log(Object message) {
for (ILogger logger : loggers) {
logger.log(message);
@Override
public void log(Object message)
{
for (ILogger logger : loggers) {
logger.log(message);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,56 @@
* <p>
* The format - and it's semantics - is defined by the header: {@link FileMemoryLogger#FILEHEADER}.
*/
public class FileMemoryLogger extends AbstractLogger {
public class FileMemoryLogger
extends AbstractLogger
{

private static final String FILEHEADER = "\"Date/Time\",\"Total Memory\",\"Free Memory\",\"Message\"";
private static final String FILEHEADER = "\"Date/Time\",\"Total Memory\",\"Free Memory\",\"Message\"";

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final Logger logger = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass());

private static final SimpleDateFormat FILENAME_FORMAT = new SimpleDateFormat(
"yyyyMMdd_HHmmss");
private static final SimpleDateFormat DATEFIELD_FORMAT = new SimpleDateFormat(
"yyyy.MM.dd HH:mm:ss");
private static final SimpleDateFormat FILENAME_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss");
private static final SimpleDateFormat DATEFIELD_FORMAT = new SimpleDateFormat(
"yyyy.MM.dd HH:mm:ss");

public static String now(SimpleDateFormat format) {
return format.format(new Date());
}
public static String now(SimpleDateFormat format)
{
return format.format(new Date());
}

private PrintStream output;
private PrintStream output;

public FileMemoryLogger() {
public FileMemoryLogger()
{

try {
output = new PrintStream(new BufferedOutputStream(new FileOutputStream(FILENAME_FORMAT
.format(new Date()).concat(".txt"))));
output.println(FILEHEADER);
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
output = null;
}
try {
output = new PrintStream(new BufferedOutputStream(
new FileOutputStream(FILENAME_FORMAT.format(new Date()).concat(".txt"))));
output.println(FILEHEADER);
}
catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
output = null;
}

}
}

@Override
public void logObject(Object message) {
if (output != null) {
output.println("\"" + DATEFIELD_FORMAT.format(new Date()) + "\",\""
+ Runtime.getRuntime().totalMemory() + "\",\""
+ Runtime.getRuntime().freeMemory() + "\",\"" + message
+ "\"");
@Override
public void logObject(Object message)
{
if (output != null) {
output.println("\"" + DATEFIELD_FORMAT.format(new Date()) + "\",\""
+ Runtime.getRuntime().totalMemory() + "\",\""
+ Runtime.getRuntime().freeMemory() + "\",\"" + message + "\"");
}
}
}

@Override
protected void finalize() throws Throwable {
output.close();
super.finalize();
}
@Override
protected void finalize() throws Throwable
{
output.close();
super.finalize();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
/**
* Represents a simplistic facade to direct messages to different logging endpoints.
*/
public interface ILogger {
public interface ILogger
{

/**
* Direct {@code message} to the underlying logging endpoint. The {@code message} can be a {@link Throwable} or in
* a classical representation, e.g. {@link String} or {@link CharSequence}.
*
* @param message Must not be {@code null}.
*/
void log(Object message);
/**
* Direct {@code message} to the underlying logging endpoint. The {@code message} can be a
* {@link Throwable} or in a classical representation, e.g. {@link String} or
* {@link CharSequence}.
*
* @param message
* Must not be {@code null}.
*/
void log(Object message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,56 @@
import java.io.InputStream;
import java.io.OutputStream;

public class InputStreamSpy extends InputStream {
public class InputStreamSpy
extends InputStream
{

private final InputStream iStream;
private final OutputStream oStream;
private final InputStream iStream;
private final OutputStream oStream;

public InputStreamSpy(InputStream iStream, OutputStream oStream) {
this.iStream = iStream;
this.oStream = oStream;
}
public InputStreamSpy(InputStream iStream, OutputStream oStream)
{
this.iStream = iStream;
this.oStream = oStream;
}

@Override
public int read() throws IOException {
int result = iStream.read();
oStream.write(result);
return result;
}
@Override
public int read() throws IOException
{
int result = iStream.read();
oStream.write(result);
return result;
}

@Override
public int available() throws IOException {
return iStream.available();
}
@Override
public int available() throws IOException
{
return iStream.available();
}

@Override
public void close() throws IOException {
iStream.close();
oStream.flush();
}
@Override
public void close() throws IOException
{
iStream.close();
oStream.flush();
}

@Override
public void mark(int readlimit) {
iStream.mark(readlimit);
}
@Override
public void mark(int readlimit)
{
iStream.mark(readlimit);
}

@Override
public void reset() throws IOException {
iStream.reset();
}

@Override
public boolean markSupported() {
return iStream.markSupported();
}
@Override
public void reset() throws IOException
{
iStream.reset();
}

@Override
public boolean markSupported()
{
return iStream.markSupported();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@
/**
* A logger implementation which directs to Slf4J.
*/
public class Slf4JLogger extends AbstractLogger {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public class Slf4JLogger
extends AbstractLogger
{
private static final Logger logger = LoggerFactory
.getLogger(MethodHandles.lookup().lookupClass());

@Override
protected void logObject(Object message) {
if (isThrowable(message.getClass())) {
logger.info(createThrowableMessage((Throwable) message));
} else if (message instanceof String) {
logger.info((String) message);
} else {
// Choosing a different level and pre-text here as this might not have been intended.
logger.warn("Logging {}: {}", message.getClass(), message);
@Override
protected void logObject(Object message)
{
if (isThrowable(message.getClass())) {
logger.info(createThrowableMessage((Throwable) message));
}
else if (message instanceof String) {
logger.info((String) message);
}
else {
// Choosing a different level and pre-text here as this might not have been intended.
logger.warn("Logging {}: {}", message.getClass(), message);
}
}
}
}
Loading

0 comments on commit 1b9a679

Please sign in to comment.