Friday, 24 February 2023

Factory design pattern

 public class CSVFactory {

    public static void main(String arg[]) {
// GenerateCSVFile filetype= CSVFactoryClass
CSVFactoryClass factorydata = new CSVFactoryClass();
factorydata.callCSVfactory("detail").generateCSVFile();
CompanyWideReportCSV abc= new CompanyWideReportCSV();
abc= (CompanyWideReportCSV) factorydata.callCSVfactory("company");
abc.generateCSVFile();
}
}
interface GenerateCSVFile {
public String generateCSVFile();
}

class DetailReportCSV implements GenerateCSVFile{
public String generateCSVFile(){
System.out.println("pawan kumar------");
return "pawan";
}

public String generateCSVFilea(){
System.out.println("pawan kumar1111111111----------");
return "pawan";
}

}

class CompanyWideReportCSV implements GenerateCSVFile{
public String generateCSVFile(){
System.out.println("----------kumar----------");
return "kumar";
}
}

class CSVFactoryClass {
public GenerateCSVFile callCSVfactory(String csvType) {
if(csvType.equalsIgnoreCase("detail")){
return new DetailReportCSV();
}else if(csvType.equalsIgnoreCase("company")){
return new CompanyWideReportCSV();
} else {
return new DetailReportCSV();
}
}
}

Friday, 17 February 2023

Stream pratical example

  public static void main (String arg[]) {

//        Stream.of("a2", "a1", "b1", "b3", "c2")
// .filter(s -> {
// System.out.println("filter: " + s);
// //return true;
// if(s.startsWith("a")){
// return true;
// }
// return false;
// })
// .forEach(s -> System.out.println("forEach: " + s));
// SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Date date = new Date();
// long startTime = Instant.now().toEpochMilli();
// log.info("shop-game-usage-report_scheduler Start time" + startTime);
// Stream.of("ananas", "oranges", "apple", "pear", "banana")
// .map(String::toUpperCase) // 1. Process
// .sorted() // 2. Sort
// .filter(s -> s.startsWith("A")) // 3. Reject
// .forEach(System.out::println);
// long totalTimeTaken = Instant.now().toEpochMilli() - startTime;
// log.info("shop-game-usage-report_scheduler time taken" + totalTimeTaken);
// System.out.println("-----------------------------------------------");
// log.info("shop-game-usage-report_scheduler Start time" + startTime);
// Stream.of("ananas", "oranges", "apple", "pear", "banana").filter(s->s.startsWith("a")).map(s->s.toUpperCase()).sorted((l,r)->l.compareTo(r)).forEach(s-> System.out.println(s));
// log.info("shop-game-usage-report_scheduler time taken" + totalTimeTaken);



// Stream.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16).filter(number->number%2==0).forEach(evenNumber-> System.out.println(evenNumber));

List<Integer> number= Arrays.asList(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,7,8,9);
//Set<Integer> duplicate= new HashSet<>();
//Set<Integer>ghy=number.stream().filter(n->!duplicate.add(n)).collect(Collectors.toSet());
//System.out.println(ghy);
//System.out.println(number.stream().findFirst().get());
//System.out.println(number.stream().findAny().get());
//System.out.println(number.stream().count());
// List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
// Set<Integer> set = new HashSet();
// myList.stream()
// .filter(n -> !set.add(n))
// .forEach(System.out::println);
// System.out.println(number.stream().max(Integer::compare).get());
// System.out.println(number.stream().min(Integer::compare).get());
// Map<String,String> charkey= new HashMap();
// String name="pawankumar";
// name.chars().mapToObj(c->(char)c).forEach(c->{
// charkey.put(c.toString(),c.toString());
// });

// String input = "Java Hungry jBlog Alive is Awesome";
//
// Character result = input.chars() // Stream of String
// .mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s))) // First convert to Character object and then to lowercase
// .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting())) //Store the chars in map with count
// .entrySet()
// .stream()
// .filter(entry -> entry.getValue() == 1L)
// .map(entry -> entry.getKey())
// .findFirst()
// .get();
// System.out.println(result);


// List<Integer> myList = Arrays.asList(10,15,8,49,25,98,32);
// myList.stream()
// .map(s -> s + "") // Convert integer to String
// .forEach(System.out::println);

// List<Integer> myList = Arrays.asList(10,15,8,49,25,98,98,32,15);
// myList.stream().collect(Collectors.toSet()).stream().sorted(Collectors.reverseOrder()).forEach(s-> System.out.println(s));

// myList.stream()
// .collect(Collectors.toSet()).stream().sorted()
// .forEach(System.out::println);


class Student {

// Instance Variables
// Properties of a student
String rollNo;
String name;

// Constructor
Student(String rollNo, String name) {
this.rollNo = rollNo;
this.name = name;
}

// To test the equality of two Student objects

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return rollNo.equals(student.rollNo);
}

@Override
public int hashCode() {
return Objects.hash(rollNo);
}
}


// Create three Student objects
Student s1 = new Student("pawan", "Ram");
Student s2 = new Student("kumar", "Shyam");
Student s3 = new Student("pawan", "pawanrt");
System.out.println("-----"+s1.hashCode());
System.out.println("-----"+s2.hashCode());
System.out.println("-----"+s3.hashCode());
// Create a HashSet to store Student instances
HashSet<Student> studentData = new HashSet<Student>();

// Adding Student objects
studentData.add(s1);

// s2 will NOT be inserted because it has the same hashcode as s1
// and it satisfies the condition of equals() method with s1
// i.e. s1.equals(s2) == true
studentData.add(s2);

// s3 will be inserted as it has different hashcode than s1
studentData.add(s3);

// Print the elements of studentData HashSet
for (Student s : studentData) {
System.out.println(s.rollNo + " " + s.name);
}
}

Thursday, 16 February 2023

stream API example

 https://javahungry.blogspot.com/2020/05/java-8-coding-and-programming-interview-questions.html

Tuesday, 7 February 2023

Cron job scheduling example

 https://reflectoring.io/spring-scheduler/

Grouping by example

 public static void main(String arg[]) {

    final AtomicInteger counter = new AtomicInteger(0);
List<Integer> questionsList = new ArrayList<>();
for(Integer i=0;i<100;i++) {

questionsList.add(i*2);
}
final Collection<List<Integer>> dividedByTenList = questionsList.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / 10))
.values();
dividedByTenList.forEach(batchlist -> {
System.out.println("hello pawan"+batchlist);
});
}

Wednesday, 1 February 2023

verify cognito token

 https://openid.net/developers/jwt/

jose4j

  • Open source implementation of JWT and the full JOSE suite. Developed by Brian Campbell.
  • License: Apache 2.0
  • Supports: JWT, JWS, JWE and JWK.
  • Target Environment: Java 7 or 8

Nimbus JOSE+JWT