Subversion Repositories SmartDukaan

Rev

Rev 21675 | Rev 21792 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21543 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
3
import java.time.LocalDate;
21652 ashik.ali 4
import java.time.LocalDateTime;
21543 ashik.ali 5
import java.time.LocalTime;
6
import java.time.format.DateTimeParseException;
21570 ashik.ali 7
import java.util.ArrayList;
8
import java.util.List;
21543 ashik.ali 9
 
10
import javax.mail.internet.InternetAddress;
11
 
21570 ashik.ali 12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14
 
15
import com.fasterxml.jackson.core.JsonProcessingException;
16
import com.fasterxml.jackson.databind.ObjectMapper;
17
 
21543 ashik.ali 18
public class StringUtils {
21570 ashik.ali 19
 
20
	private static ObjectMapper objectMapper = new ObjectMapper();
21
 
22
	private static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class);
23
 
21543 ashik.ali 24
	private StringUtils(){
25
 
26
	}
27
	public static final LocalDate toDate(String dateString)throws DateTimeParseException{
28
		return LocalDate.parse(dateString);
29
	}
30
 
31
	public static final LocalTime toTime(String timeString) throws DateTimeParseException{
32
		return LocalTime.parse(timeString);
33
	}
21652 ashik.ali 34
 
35
	public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
21655 ashik.ali 36
		if(dateTimeString == null){
37
			return null;
38
		}
21652 ashik.ali 39
		return LocalDateTime.parse(dateTimeString);
40
	}
21543 ashik.ali 41
 
42
	public static boolean isValidMobile(String mobile){
43
		try{
44
			Long.valueOf(mobile);
45
		}
46
		catch(Exception e){
47
			return false;
48
		}
49
 
50
		if (mobile.startsWith("0")){
51
			return false;
52
		}
53
		if (mobile.length()!=10){
54
			return false;
55
		}
56
		return true;
57
	}
58
 
59
	public static boolean isValidEmailAddress(String email) {
60
		boolean result = true;
61
		try {
62
			InternetAddress emailAddr = new InternetAddress(email);
63
			emailAddr.validate();
64
		} catch (Exception ex) {
65
			result = false;
66
		}
67
		return result;
68
	}
21570 ashik.ali 69
 
70
	public static List<String> getDuplicateElements(List<String> elements){
71
		List<String> duplicates = new ArrayList<>();
72
		for(int i = 0; i < elements.size(); i++){
73
			for(int j = i + 1; j < elements.size(); j++){
74
				if(elements.get(i).equals(elements.get(j))){
75
					duplicates.add(elements.get(i));
76
				}
77
			}
78
		}
79
		return duplicates;
80
	}
81
 
82
	public static String toString(Object object) throws Exception{
83
		try {
84
			return objectMapper.writeValueAsString(object);
85
		} catch (JsonProcessingException e) {
86
			LOGGER.error("Error occured while converting object to json", e);
87
			throw e;
88
		}
89
	}
21756 ashik.ali 90
	public static boolean isValidPinCode(String pinCode){
91
		if(pinCode == null || pinCode.isEmpty()){
92
			return false;
93
		}
94
		if(pinCode.length() != 6){
95
			return false;
96
		}
97
		if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
98
			return false;
99
		}
100
		return true;
101
 
102
	}
21675 ashik.ali 103
 
21543 ashik.ali 104
}