diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 1fed347..b236532 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/thriftCompiler.xml b/.idea/thriftCompiler.xml new file mode 100644 index 0000000..7bc123c --- /dev/null +++ b/.idea/thriftCompiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/common/src/main/java/com/ossez/framework/common/tasks/Task1.java b/common/src/main/java/com/ossez/framework/common/tasks/Task1.java new file mode 100644 index 0000000..252e553 --- /dev/null +++ b/common/src/main/java/com/ossez/framework/common/tasks/Task1.java @@ -0,0 +1,40 @@ +package com.ossez.framework.common.tasks; + +import org.apache.commons.lang.WordUtils; +import org.apache.commons.lang3.StringUtils; + +public class Task1 { + + + public static int getClosestToZero(int[] a) { + /* + Please implement this method to + return the number in the array that is closest to zero. + If there are two equally far from zero elements like 2 and -2, + consider the positive element to be "closest" to zero. + + Please add a main method which evaluates and displays one or more test cases. + */ + + int index = Math.abs(0 - a[0]); + int result = a[0]; + for (int i : a) { + int abs = Math.abs(0 - i); + if (abs <= index) { + index = abs; + result = i; + } + } + return result; + + + } + + public static void main(String[] args) { + int[] in = {}; + System.out.println(getClosestToZero(in)); + + } + + +} diff --git a/common/src/main/java/com/ossez/framework/common/tasks/Task2.java b/common/src/main/java/com/ossez/framework/common/tasks/Task2.java new file mode 100644 index 0000000..2ff49a1 --- /dev/null +++ b/common/src/main/java/com/ossez/framework/common/tasks/Task2.java @@ -0,0 +1,68 @@ +package com.ukg.framework.common.tasks; + +import org.apache.commons.lang.WordUtils; + +public class Task2 { + + public static String capitalizeFirstLetters(String s) { + /* + * Please implement this method to capitalize all first letters of the + * words in the given String. All other symbols shall remain intact. If + * a word does not start with a letter, it shall remain intact also. + * Assume that the parameter String can only contain spaces and + * alphanumeric characters. + * + * NOTE: please keep in mind that the words can be divided by single or + * multiple spaces. The spaces also can be found at the beginning or the + * end of the parameter string, and you need to preserve them. + * + * Please add a main method which evaluates and displays one or more + * test cases. + */ + + char[] delimiters = null; + + if (s == null || s.length() == 0) { + return s; + } + int strLen = s.length(); + StringBuffer buffer = new StringBuffer(strLen); + boolean capitalizeNext = true; + for (int i = 0; i < strLen; i++) { + char ch = s.charAt(i); + + if (isDelimiter(ch, delimiters)) { + buffer.append(ch); + capitalizeNext = true; + } else if (capitalizeNext) { + buffer.append(Character.toTitleCase(ch)); + capitalizeNext = false; + } else { + buffer.append(ch); + } + } + return buffer.toString(); + } + + private static boolean isDelimiter(char ch, char[] delimiters) { + if (delimiters == null) { + return Character.isWhitespace(ch); + } + for (int i = 0, isize = delimiters.length; i < isize; i++) { + if (ch == delimiters[i]) { + return true; + } + } + return false; + } + + public static void main(String[] args) { + String in = " 2 drer drse 23 wer 1 "; + System.out.println(capitalizeFirstLetters(in)); + + // FYI: Apache WordUtils has capitalize function to do this. + // System.out.println(WordUtils.capitalize(in)); + } + + +} diff --git a/common/src/main/java/com/ossez/framework/common/tasks/Task3.java b/common/src/main/java/com/ossez/framework/common/tasks/Task3.java new file mode 100644 index 0000000..5eca133 --- /dev/null +++ b/common/src/main/java/com/ossez/framework/common/tasks/Task3.java @@ -0,0 +1,32 @@ +package com.ukg.framework.common.tasks; + +public class Task3 { + + + + // Please do not change this interface + + interface ListNode { + int getItem(); + ListNode getNext(); + void setNext(ListNode next); + } + + public static ListNode reverse(ListNode node) { + /* + Please implement this method to + reverse a given linked list. + */ + + ListNode temp = null, prev = null; + while(node != null){ + temp = node.getNext(); + node.setNext(prev); + prev = node; + node = temp; + + } + return prev; + } + +} diff --git a/common/src/main/java/com/ossez/framework/common/tasks/Task4.java b/common/src/main/java/com/ossez/framework/common/tasks/Task4.java new file mode 100644 index 0000000..c37134f --- /dev/null +++ b/common/src/main/java/com/ossez/framework/common/tasks/Task4.java @@ -0,0 +1,103 @@ +package com.ukg.framework.common.tasks; + +import org.apache.commons.lang3.ObjectUtils; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + +import java.io.Serializable; +import java.time.DateTimeException; +import java.util.Date; +import java.util.Objects; + + +public class Task4 { + + + static class TimeRange implements Serializable { + private static final long serialVersionUID = 6474765081240948885L; + /* + * Design a class that will represent a time range: start time to end time + * One possible use for such a class is in scheduling or booking application. + * Example: Conference room will be used from Monday 10am to 12pm + * Create any code constructors, variables and methods that you feel are necessary. + * One such method should test for intersection or conflict + * in times between 2 instances of TimeRange + * + */ + private Date startDateTime; + private Date endDateTime; + + public TimeRange(Date startDateTime, Date endDateTime) { + this.startDateTime = startDateTime; + this.endDateTime = endDateTime; + } + + public Date getStartDateTime() { + return startDateTime; + } + + public void setStartDateTime(Date startDateTime) { + this.startDateTime = startDateTime; + } + + public Date getEndDateTime() { + return endDateTime; + } + + public void setEndDateTime(Date endDateTime) { + this.endDateTime = endDateTime; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TimeRange timeRange = (TimeRange) o; + return Objects.equals(startDateTime, timeRange.startDateTime) && Objects.equals(endDateTime, timeRange.endDateTime); + } + + @Override + public int hashCode() { + return Objects.hash(startDateTime, endDateTime); + } + + public Boolean isConflict(TimeRange tr2) throws DateTimeException { + + if (this.isValidRange() && tr2.isValidRange()) { + if (tr2.startDateTime.before(this.endDateTime)) + return true; + else + return false; + } else { + throw new DateTimeException("Range Error "); + } + } + + /** + * @return + */ + public Boolean isValidRange() throws DateTimeException { + + if (ObjectUtils.isNotEmpty(startDateTime) && ObjectUtils.isNotEmpty(endDateTime) && startDateTime.before(endDateTime)) { + return true; + } else { + throw new DateTimeException("Time Range Error "); + } + } + } + + public static void main(String[] args) { + DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); + + + TimeRange tr1 = new TimeRange(DateTime.parse("2021-12-09 08:00:01", formatter).toDate(), DateTime.parse("2021-12-09 10:00:01", formatter).toDate()); + TimeRange tr2 = new TimeRange(DateTime.parse("2021-12-09 08:00:01", formatter).toDate(), DateTime.parse("2021-12-09 08:30:01", formatter).toDate()); + try { + System.out.println(tr1.isConflict(tr2)); + } catch (DateTimeException e) { + e.printStackTrace(); + } + } + +} diff --git a/common/src/test/java/com/ossez/framework/common/tests/dao/SearchTest.java b/common/src/test/java/com/ossez/framework/common/tests/dao/SearchTest.java new file mode 100644 index 0000000..2f781b5 --- /dev/null +++ b/common/src/test/java/com/ossez/framework/common/tests/dao/SearchTest.java @@ -0,0 +1,253 @@ +package com.ossez.framework.common.tests.dao; + + +import com.ossez.framework.common.dao.Factory; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.compress.utils.Lists; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.hibernate.annotations.Synchronize; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.validation.ValidationException; +import java.util.*; + +/** + * ReservationTest Testing + * + * @author YuCheng Hu + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SearchTest { + private static Logger logger = LoggerFactory.getLogger(SearchTest.class); + HashMap tokenMap = new HashMap<>(); + HashMap queryToken = new HashMap<>(); + + @BeforeEach + protected void setUp() throws Exception { + logger.debug("-OPEN Database Connection-"); +// Factory.beginTransaction(); + } + + @AfterEach + protected void tearDown() throws Exception { + logger.debug("-CLOSE Database Connection-"); +// Factory.commitTransaction(); + } + + /** + * Tests Index + */ + @Test + public void testIndex() throws ValidationException { + // DO INDEX + logger.debug(indexDocument("index 1 soup tomato cream salt")); + logger.debug(indexDocument("index 2 cake sugar eggs flour sugar cocoa cream butter")); + logger.debug(indexDocument("index 1 bread butter salt")); + logger.debug(indexDocument("index 3 soup fish potato salt pepper")); + logger.debug("Size of token map - [{}]", tokenMap.size()); + + + // LOAD DATA TO RAM + convertToken(); + + // DO QUERY +// logger.debug(query("in: query butter")); +// logger.debug(query("in: query sugar")); +// logger.debug(query("in: query soup")); + logger.debug(query("in: query (butter | potato) & (salt | tea)")); + logger.debug("Size of query Token - [{}]", queryToken.size()); + } + + /** + * Tests Query + */ + @Test + public void testQuery() throws ValidationException { + + } + + + /** + * Build reservationRequest + * + * @return + */ + private String indexDocument(String token) { + + List tokenList = Arrays.asList(StringUtils.split(token)); + + if (tokenList.size() < 3) + return "index error: The format of input error. Please using format: [index 1 soup tomato cream salt]"; + + if (!StringUtils.equalsIgnoreCase(tokenList.get(0), "Index")) + return "index error: The format of input error. Please using Index to start it"; + + if (!NumberUtils.isCreatable(tokenList.get(1))) + return "index error: The format of input error. The Doc Index must Number"; + + + Integer indexId = NumberUtils.toInt(tokenList.get(1)); + List tokens = tokenList.subList(2, tokenList.size()); + + + if (ObjectUtils.isNotEmpty(tokenMap.get(indexId))) + tokenMap.remove(indexId); + + tokenMap.put(indexId, tokens); + + + return "index ok " + indexId; + } + + /** + * Build reservationRequest + * + * @return + */ + private String query(String q) { + List qList = Arrays.asList(StringUtils.split(q)); + if (qList.size() < 3) + return "query error: The format of input error. Please using format: [in: query butter]"; + + if (!StringUtils.equalsIgnoreCase(qList.get(0), "in:")) + return "index error: The format of input error. Please using [in:] to start it"; + + if (!StringUtils.equalsIgnoreCase(qList.get(1), "query")) + return "index error: The format of input error. String of command is [query]"; + + q = StringUtils.replace(q, StringUtils.SPACE, StringUtils.EMPTY); + q = StringUtils.replace(q, "in:query", StringUtils.EMPTY); + List queryList = processQueryString(q); + + // ONLY SEARCH 1 TOKEN + if (queryList.size() == 1) { + return "query results " + Arrays.toString(queryToken.get(queryList.get(0)).toArray()); + } + + + + Set result = new HashSet<>(); + result.clear(); + + for (String qStr : queryList) { + qStr = StringUtils.remove(qStr, "("); + qStr = StringUtils.remove(qStr, ")"); + + logger.debug(">>>>>>>>>>{}", qStr); +// if (StringUtils.contains(qStr, "|") && !StringUtils.startsWith(qStr, "|")) { +// result = orCal(StringUtils.split(qStr, "|")[0], StringUtils.split(qStr, "|")[1]); +// } +// +// if (StringUtils.contains(qStr, "&") && !StringUtils.startsWith(qStr, "&")) { +// result = andCal(StringUtils.split(qStr, "&")[0], StringUtils.split(qStr, "&")[1]); +// } + +// if (StringUtils.startsWith(qStr, "|") || StringUtils.endsWith(qStr,"|")) { +// result.addAll(queryToken.get(StringUtils.substringAfter(qStr, "|"))); +// } +// +// if (StringUtils.startsWith(qStr, "&")|| StringUtils.endsWith(qStr,"&")) { +// result.retainAll(queryToken.get(StringUtils.substringAfter(qStr, "&"))); +// } + } + + + return "query results " + Arrays.toString(result.toArray()); + } + + /** + * LOAD INDEX + */ + private void convertToken() { + + queryToken = new HashMap(); + tokenMap.forEach((k, v) -> { + Integer docId = k; + List tokens = v; + + + for (String token : tokens) { + Set docIdset = new HashSet<>(); + if (ObjectUtils.isEmpty(queryToken.get(token))) { + docIdset.add(docId); + } else { + docIdset = queryToken.get(token); + docIdset.add(docId); + } + + queryToken.put(token, docIdset); + } + + + }); + + } + + + /** + * @return + * @throws ValidationException + */ + private List processQueryString(String q) throws ValidationException { + List opList = Lists.newArrayList(); + +// String input = "(a|b)&c"; + while (StringUtils.isNotEmpty(q)) { + String op = StringUtils.EMPTY; + + if (StringUtils.contains(q, "(") && StringUtils.contains(q, ")")) { + String opRight = StringUtils.substringAfterLast(q, "("); + int startIndex = StringUtils.lastIndexOf(q, "("); + int endIndex = startIndex + StringUtils.indexOf(opRight, ")"); + op = StringUtils.substring(q, startIndex, endIndex + 2); + } else { + op = q; + } + + opList.add(op); + q = StringUtils.remove(q, op); + } + + return opList; + } + + + /** + * OR Cal + * + * @param token1 + * @param token2 + * @return + */ + private Set orCal(String token1, String token2) { + Set result = new HashSet<>(); + result.addAll(queryToken.get(token1)); + result.addAll(queryToken.get(token2)); + + return result; + } + + /** + * AND Cal + * + * @param token1 + * @param token2 + * @return + */ + private Set andCal(String token1, String token2) { + Set result = new HashSet<>(); + result.addAll(queryToken.get(token1)); + result.retainAll(queryToken.get(token2)); + + return result; + } +} + + diff --git a/pom.xml b/pom.xml index 6e476c8..917c1b0 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,6 @@ Demo for Framework Orm Factory Query Data https://github.com/honeymoose/Framework-Orm-Factory.git - - YuCheng Hu @@ -23,7 +21,7 @@ -5 Open Source - Sr. Java Developer + Developer