![JAR search and dependency download from the Maven repository](/logo.png)
twitter4jads.util.TwitterAdUtil Maven / Gradle / Ivy
The newest version!
package twitter4jads.util;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import twitter4jads.BaseAdsListBatchPostResponse;
import twitter4jads.BaseAdsListResponse;
import twitter4jads.BaseAdsResponse;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import twitter4jads.internal.http.HttpResponse;
import twitter4jads.internal.models4j.RateLimitStatus;
import twitter4jads.models.ads.TrackingTag;
import twitter4jads.models.ads.TwitterAdObjective;
import twitter4jads.models.ads.audience.AudienceApiResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static com.google.common.collect.Sets.newHashSet;
import static org.apache.commons.collections.SetUtils.unmodifiableSet;
import static twitter4jads.models.ads.TwitterAdObjective.PREROLL_VIEWS;
import static twitter4jads.models.ads.TwitterAdObjective.WEBSITE_CONVERSIONS;
/**
*
* Date: 29/01/14
* Time: 2:23 PM
*/
public final class TwitterAdUtil {
public static final String FOLLOWERS = "FOLLOWERS";
public static final String ENGAGEMENTS = "ENGAGEMENTS";
public static final String VIDEO_VIEWS = "VIDEO_VIEWS";
public static final String WEBSITE_CLICKS = "WEBSITE_CLICKS";
public static final String VIDEO_VIEWS_PREROLL = "VIDEO_VIEWS_PREROLL";
public static final String APP_ENGAGEMENTS = "APP_ENGAGEMENTS";
public static final String APP_INSTALLS = "APP_INSTALLS";
public static final Set TAP_SUPPORTED_OBJECTIVES =
unmodifiableSet(newHashSet(APP_INSTALLS, APP_ENGAGEMENTS, PREROLL_VIEWS, WEBSITE_CLICKS, WEBSITE_CONVERSIONS));
public static final Set TARGET_CPA_SUPPORTED_OBJECTIVES = unmodifiableSet(newHashSet(WEBSITE_CONVERSIONS));
public static final String UTC_TMZ = "UTC";
public static final String FORMAT_YYYYMMDD_HHMM = "yyyyMMdd_HHmm";
public static final ThreadLocal UTC_CALENDAR = new ThreadLocal() {
@Override
protected Calendar initialValue() {
return Calendar.getInstance(TimeZone.getTimeZone(UTC_TMZ));
}
};
public static final ThreadLocal FORMATTER_UTC_YYYYMMDD_HHMM = new ThreadLocal() {
@Override
protected SimpleDateFormat initialValue() {
SimpleDateFormat rv = new SimpleDateFormat(FORMAT_YYYYMMDD_HHMM);
rv.setCalendar(UTC_CALENDAR.get());
return rv;
}
};
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 != null && 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 isEmpty(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 = TwitterAdHttpUtils.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);
if (baseResponse == null) {
return null;
}
RateLimitStatus rateLimitStatus = TwitterAdHttpUtils.createFromResponseHeader(httpResponse);
baseResponse.setRateLimitStatus(rateLimitStatus);
return baseResponse;
}
public static BaseAdsListBatchPostResponse constructBaseAdsListBatchPostResponse(HttpResponse httpResponse, String response, Type type) throws IOException {
Gson gson = new Gson();
BaseAdsListBatchPostResponse baseResponse = gson.fromJson(response, type);
RateLimitStatus rateLimitStatus = TwitterAdHttpUtils.createFromResponseHeader(httpResponse);
baseResponse.setRateLimitStatus(rateLimitStatus);
return baseResponse;
}
public static AudienceApiResponse constructAudienceApiResponse(HttpResponse httpResponse, String response) {
Gson gson = new Gson();
Type audienceApiResponseType = new TypeToken() {
}.getType();
AudienceApiResponse audienceApiResponse = gson.fromJson(response, audienceApiResponseType);
RateLimitStatus rateLimitStatus = TwitterAdHttpUtils.createFromResponseHeader(httpResponse);
audienceApiResponse.setRateLimitStatus(rateLimitStatus);
return audienceApiResponse;
}
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();
}
}
}
public static String getTrackingTagString(List trackingTags) {
if (isNotEmpty(trackingTags)) {
TrackingTag trackingTag = trackingTags.get(0);
return trackingTag.getTrackingPartner() + "-" + trackingTag.getTrackingTag();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy