twitter4j.util.TwitterAdUtil Maven / Gradle / Ivy
package twitter4j.util;
import com.google.gson.Gson;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import twitter4j.BaseAdsListResponse;
import twitter4j.BaseAdsResponse;
import twitter4j.internal.http.HttpResponse;
import twitter4j.internal.models4j.RateLimitStatus;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static twitter4j.util.TwitterAdHttpUtils.createFromResponseHeader;
/**
* User: poly
* Date: 29/01/14
* Time: 2:23 PM
*/
public final class TwitterAdUtil {
public static final String FOLLOWERS = "FOLLOWERS";
public static final String TWEET_ENGAGEMENTS = "TWEET_ENGAGEMENTS";
public static final String VIDEO_VIEWS = "VIDEO_VIEWS";
public static final String WEBSITE_CLICKS = "WEBSITE_CLICKS";
public static String convertTimeToZuluFormatAndToUTC(long time) {
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
DateTime dateTimeStart = new DateTime(time, DateTimeZone.UTC);
return dateTimeStart.toString(dateFormat);
}
public static String getCsv(Collection collection) {
String result = "";
if (collection.size() != 0) {
result = getLocalCsv(collection);
}
return result;
}
public static String getLocalCsv(Collection coll) {
StringBuilder buff = new StringBuilder();
int i = 0;
for (T value : coll) {
if (i != 0) {
buff.append(",");
}
buff.append(value);
i++;
}
return buff.toString();
}
public static boolean isNotNullOrEmpty(String string) {
return !(string == null || string.isEmpty());
}
public static boolean isNotNull(Object object) {
return object != null;
}
public static void containedInList(T object, List list, String message) {
if (list == null || !list.contains(object)) {
throw new IllegalArgumentException(message);
}
}
public static void ensureNotNull(Object object, String name) {
if (object == null) {
throw new IllegalArgumentException(name + " can not be null.");
}
}
public static void ensureNotEmpty(Collection collection, String name) {
if (!isNotEmpty(collection)) {
throw new IllegalArgumentException(name + " can not be null or empty.");
}
}
public static boolean isNotEmpty(Collection collection) {
return collection != null && collection.size() != 0;
}
public static Boolean ensureMaxSize(Collection collection, int size) {
if (isNotEmpty(collection) && size > 0) {
if (collection.size() > size) {
throw new IllegalArgumentException("Collection size must be less than " + size);
}
}
return true;
}
public static List createMutableList(Collection collection) {
List mutableList = new ArrayList<>();
if (isNotEmpty(collection)) {
for (E data : collection) {
mutableList.add(data);
}
}
return mutableList;
}
public static String getDelimiterSeparatedMethod(final Collection values, String delimiter) {
if (values == null || values.isEmpty()) {
return "";
}
String rv = "";
for (String value : values) {
rv = rv + value + delimiter;
}
rv = rv.substring(0, rv.length() - 1);
return rv;
}
public static BaseAdsResponse constructBaseAdsResponse(HttpResponse httpResponse, String response, Type type) throws IOException {
if (type == null) {
return null;
}
Gson gson = new Gson();
BaseAdsResponse baseResponse = gson.fromJson(response, type);
RateLimitStatus rateLimitStatus = createFromResponseHeader(httpResponse);
baseResponse.setRateLimitStatus(rateLimitStatus);
return baseResponse;
}
public static BaseAdsListResponse constructBaseAdsListResponse(HttpResponse httpResponse, String response, Type type) throws IOException {
Gson gson = new Gson();
BaseAdsListResponse baseResponse = gson.fromJson(response, type);
RateLimitStatus rateLimitStatus = createFromResponseHeader(httpResponse);
baseResponse.setRateLimitStatus(rateLimitStatus);
return baseResponse;
}
public static void reallySleep(long millis) {
boolean threadInterrupted = false;
final long nanos = TimeUnit.MILLISECONDS.toNanos(millis);
final long end = System.nanoTime() + nanos;
long remaining;
try {
do {
remaining = end - System.nanoTime();
if (remaining <= 0) {
break;
}
try {
Thread.sleep(TimeUnit.NANOSECONDS.toMillis(remaining));
} catch (InterruptedException e) {
threadInterrupted = true;
}
} while (remaining > 0);
} finally {
if (threadInterrupted) {
Thread.currentThread().interrupt();
}
}
}
}