Skip to content

Commit

Permalink
use Airlift code style
Browse files Browse the repository at this point in the history
  • Loading branch information
muga committed Sep 22, 2015
1 parent 8630a68 commit a464316
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 122 deletions.
117 changes: 117 additions & 0 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="FileTabCharacter"/>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\r"/>
<property name="message" value="Line contains carriage return"/>
</module>
<module name="RegexpMultiline">
<property name="format" value=" \n"/>
<property name="message" value="Line has trailing whitespace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\{\n\n"/>
<property name="message" value="Blank line after opening brace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\}"/>
<property name="message" value="Blank line before closing brace"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\n"/>
<property name="message" value="Multiple consecutive blank lines"/>
</module>
<module name="RegexpMultiline">
<property name="format" value="\n\n\Z"/>
<property name="message" value="Blank line before end of file"/>
</module>

<module name="TreeWalker">
<module name="EmptyBlock">
<property name="option" value="text"/>
<property name="tokens" value="
LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_IF,
LITERAL_FOR, LITERAL_TRY, LITERAL_WHILE, INSTANCE_INIT, STATIC_INIT"/>
</module>
<module name="EmptyStatement"/>
<module name="EmptyForInitializerPad"/>
<module name="EmptyForIteratorPad">
<property name="option" value="space"/>
</module>
<module name="MethodParamPad">
<property name="allowLineBreaks" value="true"/>
<property name="option" value="nospace"/>
</module>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="NeedBraces"/>
<module name="LeftCurly">
<property name="option" value="nl"/>
<property name="tokens" value="CLASS_DEF, CTOR_DEF, INTERFACE_DEF, METHOD_DEF"/>
</module>
<module name="LeftCurly">
<property name="option" value="eol"/>
<property name="tokens" value="
LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR,
LITERAL_IF, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE"/>
</module>
<module name="RightCurly">
<property name="option" value="alone"/>
</module>
<module name="GenericWhitespace"/>
<module name="WhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>

<module name="UpperEll"/>
<module name="DefaultComesLast"/>
<module name="ArrayTypeStyle"/>
<module name="MultipleVariableDeclarations"/>
<module name="ModifierOrder"/>
<module name="OneStatementPerLine"/>
<module name="StringLiteralEquality"/>
<module name="MutableException"/>
<module name="EqualsHashCode"/>
<module name="InnerAssignment"/>
<module name="InterfaceIsType"/>
<module name="HideUtilityClassConstructor"/>

<module name="MemberName"/>
<module name="LocalVariableName"/>
<module name="LocalFinalVariableName"/>
<module name="TypeName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="ClassTypeParameterName">
<property name="format" value="^[A-Z][0-9]?$"/>
</module>
<module name="MethodTypeParameterName">
<property name="format" value="^[A-Z][0-9]?$"/>
</module>

<module name="AvoidStarImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>

<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="ignoreEnhancedForColon" value="false"/>
<property name="tokens" value="
ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN,
BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LE,
LITERAL_ASSERT, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE,
LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE,
LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL,
PLUS, PLUS_ASSIGN, QUESTION, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN,
STAR, STAR_ASSIGN, TYPE_EXTENSION_AND"/>
</module>
</module>
</module>
17 changes: 17 additions & 0 deletions gradle/check.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
// Checkstyle
// @see https://docs.gradle.org/2.5/userguide/checkstyle_plugin.html
apply plugin: 'checkstyle'
checkstyle {
ignoreFailures = true
// @see https://github.com/facebook/presto/blob/master/src/checkstyle/checks.xml
//configFile = rootProject.file('./checkstyle.xml') // default {project.projectDir}/config/checkstyle/checkstyle.xml
}

// FindBugs
// @see https://docs.gradle.org/2.5/userguide/findbugs_plugin.html
apply plugin: 'findbugs'
findbugs {
ignoreFailures = true
}

// PMD
// @see https://docs.gradle.org/2.5/userguide/pmd_plugin.html
apply plugin: 'pmd'
tasks.withType(Pmd) {
ignoreFailures = true
reports.html.enabled true
}

// JaCoCo
// @see https://docs.gradle.org/2.5/userguide/jacoco_plugin.html
apply plugin: 'jacoco'
63 changes: 40 additions & 23 deletions src/main/java/com/treasuredata/api/TdApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void start() throws IOException
{
try {
http.start();
} catch (Exception e) {
}
catch (Exception e) {
throw new IOException("Failed to start http client", e);
}
}
Expand All @@ -101,7 +102,8 @@ public void close()
{
try {
http.stop();
} catch (Exception e) {
}
catch (Exception e) {
throw new RuntimeException("Failed to stop http client", e);
}
}
Expand Down Expand Up @@ -138,7 +140,8 @@ public TDTable createTable(String databaseName, String tableName)
return createTable(databaseName, tableName, TDTableType.LOG);
}

public TDTable createTable(String databaseName, String tableName, TDTableType tableType) {
public TDTable createTable(String databaseName, String tableName, TDTableType tableType)
{
Request request = prepareExchange(HttpMethod.POST,
buildUrl("/v3/table/create", databaseName, tableName, tableType.name().toLowerCase()));
ContentResponse response = executeExchange(request);
Expand Down Expand Up @@ -223,8 +226,8 @@ public void deleteBulkImportSession(String sessionName)

private Request prepareExchange(HttpMethod method, String url)
{
return prepareExchange(method, url, Collections.<String,String>emptyMap(),
Collections.<String,String>emptyMap());
return prepareExchange(method, url, Collections.<String, String>emptyMap(),
Collections.<String, String>emptyMap());
}

private Request prepareExchange(HttpMethod method,
Expand All @@ -248,7 +251,7 @@ private Request prepareExchange(HttpMethod method,
request.agent(config.getAgentName());
request.header("Authorization", "TD1 " + apiKey);
//request.timeout(60, TimeUnit.SECONDS);
for (Map.Entry<String, String> entry: headers.entrySet()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
request.header(entry.getKey(), entry.getValue());
}
String dateHeader = setDateHeader(request);
Expand All @@ -264,15 +267,17 @@ private static String encode(String s)
{
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}

private static final ThreadLocal<SimpleDateFormat> RFC2822_FORMAT =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
protected SimpleDateFormat initialValue()
{
return new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
}
};
Expand All @@ -288,10 +293,12 @@ private static String setDateHeader(Request request)
private static final ThreadLocal<MessageDigest> SHA1 =
new ThreadLocal<MessageDigest>() {
@Override
protected MessageDigest initialValue() {
protected MessageDigest initialValue()
{
try {
return MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-1 digest algorithm must be available but not found", e);
}
}
Expand All @@ -316,8 +323,8 @@ static String sha1HexFromString(String string)
char[] array = new char[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
int b = (int) bytes[i];
array[i*2] = hexChars[(b & 0xf0) >> 4];
array[i*2+1] = hexChars[b & 0x0f];
array[i * 2] = hexChars[(b & 0xf0) >> 4];
array[i * 2 + 1] = hexChars[b & 0x0f];
}
return new String(array);
}
Expand All @@ -333,7 +340,8 @@ private String buildUrl(String path, String... params)
sb.append("/");
sb.append(URLEncoder.encode(param, "UTF-8"));
}
} catch (UnsupportedEncodingException ex) {
}
catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
return sb.toString();
Expand Down Expand Up @@ -370,10 +378,12 @@ private ContentResponse executeExchange(Request request)
// retry on 50x and other errors
exception = new TdApiResponseException(status, response.getContent());

} catch (TdApiException e) {
}
catch (TdApiException e) {
throw e;

} catch (Exception e) {
}
catch (Exception e) {
// retry on RuntimeException
exception = e;
}
Expand All @@ -393,7 +403,8 @@ private ContentResponse executeExchange(Request request)
Thread.sleep(retryWait);
retryWait *= 2;
}
} catch (InterruptedException e) {
}
catch (InterruptedException e) {
throw new TdApiExecutionInterruptedException(e);
}
}
Expand All @@ -404,14 +415,18 @@ private String formatRequestParameterObject(Object obj)
try {
objectMapper.writeValue(bo, obj);
return new String(bo.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
} catch (IOException ex) {
}
catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
}
finally {
try {
bo.close();
} catch (IOException ex) {
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Expand All @@ -421,7 +436,7 @@ private byte[] formatPostRequestContent(String... kvs)
{
try {
StringBuilder sb = new StringBuilder();
for(int i=0; i < kvs.length; i+=2) {
for (int i = 0; i < kvs.length; i += 2) {
if (i > 0) {
sb.append("&");
}
Expand All @@ -430,7 +445,8 @@ private byte[] formatPostRequestContent(String... kvs)
.append(encode(kvs[i + 1]));
}
return sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
}
catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
}
Expand All @@ -439,7 +455,8 @@ private <T> T parseResponse(byte[] content, Class<T> valueType)
{
try {
return objectMapper.readValue(content, valueType);
} catch (IOException ex) {
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/treasuredata/api/TdApiClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.base.Optional;

public class TdApiClientConfig
implements TdApiConstants
extends TdApiConstants
{
public static class HttpProxyConfig
{
Expand Down Expand Up @@ -73,7 +73,8 @@ public Optional<HttpProxyConfig> getHttpProxyConfig()
return httpProxyConfig;
}

public String getAgentName() {
public String getAgentName()
{
return AGENT_NAME;
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/treasuredata/api/TdApiConstants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.treasuredata.api;

public interface TdApiConstants
public class TdApiConstants
{
String AGENT_NAME = "TdApiClient v0.6";
public static final String AGENT_NAME = "TdApiClient v0.6";

protected TdApiConstants()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public TdApiExecutionInterruptedException(InterruptedException cause)
}

@Override
public InterruptedException getCause() {
public InterruptedException getCause()
{
return (InterruptedException) super.getCause();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public TdApiExecutionTimeoutException(TimeoutException cause)
}

@Override
public TimeoutException getCause() {
public TimeoutException getCause()
{
return (TimeoutException) super.getCause();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TDColumnType getElementType()
@Override
public String toString()
{
return "array<"+elementType+">";
return "array<" + elementType + ">";
}

@Override
Expand Down
Loading

0 comments on commit a464316

Please sign in to comment.