This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateUtils{ | |
/* | |
* Converts java.util.Date object to a java.time.LocalDate | |
* Things are so much more convenient now! | |
*/ | |
public static LocalDate asLocalDate(final Date date){ | |
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeInterceptor extends HandlerInterceptorAdapter{ | |
@Override | |
public boolean preHandle(final HttpServletRequest request,final HttpServletResponse response,final Object handler) | |
throws Exception | |
{ | |
/*Assume the URI is user/{userId}/post/{postId} and our interceptor is registered for this URI. | |
* This map would then be a map of two elements,with keys 'userId' and 'postId' | |
*/ | |
final Map<String, String> pathVariables = (Map<String, String>) request | |
.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Jwt requires an authentication on every request.Assuming that you are storing your User objects in a relational database like MySQL, | |
// and that your API gets moderate traffic,it is not a good idea to fetch the User object directly from the database every time. | |
//Instead,once it is retreived ,we cache it locally using Spring Caching abstraction.The cache backend is Google guava. | |
public class UserServiceImpl implements UserService { //Which further extends UserDetailsService . | |
@Override | |
@Cacheable(cacheNames=CacheConstants.USER_CACHE,key="#username") // We cache this guy by its username. | |
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { | |
final User user = userRepository.findOneByEmail(username); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberUtils{ | |
public boolean isNullOrZero(final Number number){ | |
return number == null || | |
( | |
number instanceof Integer ? number.intValue() == 0 : | |
number instanceof Long ? number.longValue() == 0 : | |
number instanceof Double ? number.doubleValue() == 0 : | |
number instanceof Short ? number.shortValue() == 0 : | |
number.floatValue() == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Format : dd-MM-yyyy HH:mm | |
// Use case : LocalDateTime object to Json serialization. | |
final LocalDateTime dateTime = LocalDateTime.now() ; //or something else. | |
final int day = dateTime.getDayOfMonth(); | |
final int month = dateTime.getMonthValue(); | |
final int year = dateTime.getYear(); | |
final int hour = dateTime.getHour(); | |
final int minute = dateTime.getMinute(); | |
jsonGenerator.writeString(new StringBuilder() | |
.append(day).append("-") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Timestamp; | |
import java.time.LocalDateTime; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
@Converter(autoApply = true) | |
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> { | |
@Override | |
public Timestamp convertToDatabaseColumn(LocalDateTime ldt) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnumUtils { | |
public static <T extends Enum<T>> T getEnumFromString(final Class<T> enumClass,final String value) { | |
if(enumClass == null){ | |
throw new IllegalArgumentException("enumClass cannot be null"); | |
} | |
for (final Enum<?> enumValue : enumClass.getEnumConstants()) { | |
if (enumValue.toString().equalsIgnoreCase(value)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import com.fasterxml.jackson.core.JsonParseException; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class JsonUtils { | |
private static final ObjectMapper objMapper = new ObjectMapper(); | |
public static <T> T toObject(final String json , final Class<T> clazz) throws Exception{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PreCondition { | |
public static <T> void checkNull(final T t, final String errorMsg){ | |
if(t==null){ | |
throw new IllegalArgumentException(errorMsg); | |
} | |
} | |
public static void checkEmptyString(final String str, final String errorMsg){ | |
if(!Strings.hasText(str)){ | |
throw new IllegalArgumentException(errorMsg); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Directories # | |
/build/ | |
bin/ | |
repos/ | |
/repos/ | |
doc/ | |
/doc/ | |
.gradle/ | |
/bin/ | |
target/ |
OlderNewer