├── doc ├── dagger │ └── MyDagger.md ├── transaction │ └── BasicClass.md ├── aop │ └── Aop.md ├── bean │ ├── BasicClass.md │ ├── IntegrateBeanFactory.md │ ├── BeanFactory.md │ ├── CustomizeBeanScan.md │ ├── HoldAllBean.md │ ├── ResolveDependency.md │ └── CustomizeBeanAnnotation.md ├── menu │ └── Menu.md └── springboot │ └── import.md ├── .gitignore ├── src ├── main │ ├── java │ │ └── org │ │ │ └── wcong │ │ │ └── test │ │ │ ├── tomcat │ │ │ └── welcome.txt │ │ │ ├── algorithm │ │ │ ├── leetcode │ │ │ │ ├── package-info.java │ │ │ │ ├── tree │ │ │ │ │ ├── TreeNode.java │ │ │ │ │ ├── ValidateBinaryTree.java │ │ │ │ │ ├── InorderSuccessorInBST.java │ │ │ │ │ ├── SumOfLeftLeaves.java │ │ │ │ │ ├── BinaryTreePaths.java │ │ │ │ │ ├── MaximumPathSum.java │ │ │ │ │ ├── BinaryTreeMaximumSubPath.java │ │ │ │ │ └── InvertBinaryTree.java │ │ │ │ ├── string │ │ │ │ │ ├── StringMultiply.java │ │ │ │ │ ├── StrongPassword.java │ │ │ │ │ ├── DecodeWays.java │ │ │ │ │ ├── TextJustifacation.java │ │ │ │ │ ├── ShortestPalindrome.java │ │ │ │ │ └── LongestSubStringContainKDistinctWord.java │ │ │ │ ├── array │ │ │ │ │ ├── TwoSum.java │ │ │ │ │ └── NumbersOfIslands.java │ │ │ │ └── dp │ │ │ │ │ ├── UniqueBinarySearchTree.java │ │ │ │ │ └── WordBreak.java │ │ │ ├── RBTree.java │ │ │ ├── dp │ │ │ │ ├── AllPermutation.java │ │ │ │ ├── ChangeMaking.java │ │ │ │ ├── CoinRow.java │ │ │ │ ├── CoinsCollect.java │ │ │ │ ├── Knapsack.java │ │ │ │ ├── RodCutting.java │ │ │ │ ├── EditDistance.java │ │ │ │ ├── BreakingAString.java │ │ │ │ ├── LongestCommonSubSequence.java │ │ │ │ ├── MatrixChainMultiplication.java │ │ │ │ ├── PrintNearly.java │ │ │ │ └── LongestPalindromeSubsequence.java │ │ │ ├── cracking │ │ │ │ ├── dp_divide_conque │ │ │ │ │ ├── TowerOfHanoi.java │ │ │ │ │ ├── TripleStep.java │ │ │ │ │ ├── BooleanEvaluation.java │ │ │ │ │ ├── MagicIndex.java │ │ │ │ │ └── Parenthesis.java │ │ │ │ ├── linked_list │ │ │ │ │ ├── SumList.java │ │ │ │ │ ├── DeleteMiddle.java │ │ │ │ │ ├── LoopDetection.java │ │ │ │ │ └── Palindrome.java │ │ │ │ ├── array_and_string │ │ │ │ │ ├── CheckPermutation.java │ │ │ │ │ ├── URLify.java │ │ │ │ │ ├── PalindromePermutation.java │ │ │ │ │ └── IsUnique.java │ │ │ │ ├── graph │ │ │ │ │ ├── BuildOrder.java │ │ │ │ │ ├── PathWithSum.java │ │ │ │ │ ├── BinarySequences.java │ │ │ │ │ └── FirstCommonAncestor.java │ │ │ │ ├── Successor.java │ │ │ │ ├── ValidateBST.java │ │ │ │ └── sort_and_search │ │ │ │ │ ├── MergeSorted.java │ │ │ │ │ ├── SparseSearch.java │ │ │ │ │ └── GroupAnagrams.java │ │ │ ├── graph │ │ │ │ ├── TopologicalSort.java │ │ │ │ ├── Vertices.java │ │ │ │ ├── Edge.java │ │ │ │ └── Graph.java │ │ │ ├── jzoffer │ │ │ │ ├── util │ │ │ │ │ ├── Tree.java │ │ │ │ │ ├── TreeNode.java │ │ │ │ │ ├── LinkedListNode.java │ │ │ │ │ ├── ComplexListNode.java │ │ │ │ │ ├── BuildLinkedList.java │ │ │ │ │ ├── LinkedList.java │ │ │ │ │ └── BuildTree.java │ │ │ │ ├── linked_list │ │ │ │ │ ├── PrintLinkedListFromBottom.java │ │ │ │ │ ├── DeleteNodeInO1.java │ │ │ │ │ ├── ReversLinkedList.java │ │ │ │ │ ├── LastKNodeInLinkedList.java │ │ │ │ │ └── ComplexListNodeCopy.java │ │ │ │ ├── dp │ │ │ │ │ ├── Fibonacci.java │ │ │ │ │ └── UglyNumber.java │ │ │ │ ├── stack │ │ │ │ │ ├── O1MinStack.java │ │ │ │ │ ├── TwoStackForQueue.java │ │ │ │ │ └── StackPushPopRule.java │ │ │ │ ├── tree │ │ │ │ │ ├── MirrorOfBinaryTree.java │ │ │ │ │ ├── PrintBinaryTreeTopDown.java │ │ │ │ │ ├── ChangeBinaryTreeToDQueue.java │ │ │ │ │ ├── BinaryTreePostOrderWalkRule.java │ │ │ │ │ └── IsSubTree.java │ │ │ │ ├── array │ │ │ │ │ ├── MoreThanHalfInArray.java │ │ │ │ │ ├── MaxSumSubArray.java │ │ │ │ │ ├── ReplaceBlank.java │ │ │ │ │ ├── SortedArraySumTarget.java │ │ │ │ │ ├── ReArrangeOddAndEven.java │ │ │ │ │ ├── CountInSortedArray.java │ │ │ │ │ └── SortedArraySumTargetSubSerial.java │ │ │ │ ├── PrintNumberOfN.java │ │ │ │ └── binary │ │ │ │ │ ├── Count1InInt.java │ │ │ │ │ └── TwoDifferentNumInArray.java │ │ │ ├── DubboLinkedInt.java │ │ │ ├── ThreeSumClosed.java │ │ │ ├── LongPrefix.java │ │ │ ├── basic │ │ │ │ ├── LinkedList.java │ │ │ │ └── Graph.java │ │ │ ├── HeapSort.java │ │ │ └── ContainerWithMostWater.java │ │ │ ├── hackerrank │ │ │ └── package-info.java │ │ │ ├── rxjava │ │ │ └── package-info.java │ │ │ ├── pattern │ │ │ ├── package-info.java │ │ │ └── creational │ │ │ │ └── Multition.java │ │ │ ├── datastructure │ │ │ ├── package-info.java │ │ │ └── LinkedList.java │ │ │ ├── mydagger │ │ │ ├── MyProvidesTest1.java │ │ │ ├── MyComponentTest.java │ │ │ └── MyProvidesTest.java │ │ │ ├── spring │ │ │ ├── aop │ │ │ │ ├── Proceed.java │ │ │ │ ├── MyAspect.java │ │ │ │ ├── MethodInterceptorImpl.java │ │ │ │ ├── AbstractAdvisor.java │ │ │ │ └── CustomizeAspectTest.java │ │ │ ├── scan │ │ │ │ ├── ScanClass1.java │ │ │ │ └── CustomizeComponent.java │ │ │ ├── jpa │ │ │ │ └── DbStockRepository.java │ │ │ ├── FullInject.java │ │ │ ├── MyInject.java │ │ │ ├── MyComponent.java │ │ │ ├── MyBefore.java │ │ │ ├── MyRequestBody.java │ │ │ ├── MyResponseBody.java │ │ │ ├── MyExceptionHandler.java │ │ │ ├── MyControllerAdvice.java │ │ │ ├── MyController.java │ │ │ ├── CollectionsUtilTest.java │ │ │ ├── annotation │ │ │ │ └── DefaultAnnotationNameTest.java │ │ │ ├── mybatis │ │ │ │ ├── mapper │ │ │ │ │ └── DbTest.java │ │ │ │ └── model │ │ │ │ │ └── TestModel.java │ │ │ ├── MyRequestMapping.java │ │ │ ├── ComponentAnnotationTest.java │ │ │ ├── BeanFactoryTest.java │ │ │ ├── ApplicationListenerTest.java │ │ │ ├── MultipleTransactionManager.java │ │ │ ├── AnnotationTest.java │ │ │ ├── CustomizeAutowiredTest.java │ │ │ ├── CacheTest.java │ │ │ ├── BeanPostProcessorTest.java │ │ │ └── InjectListOrderTest.java │ │ │ ├── autovalue │ │ │ ├── MyAutoValueClassTest.java │ │ │ ├── AutoValueTest.java │ │ │ ├── MyClass.java │ │ │ ├── MyAnnotationImpl.java │ │ │ ├── MyAnnotation.java │ │ │ ├── AutoAnnotationTest.java │ │ │ └── MyAutoValueTest.java │ │ │ ├── javaformat │ │ │ ├── FormatTest.java │ │ │ ├── RangeTest.java │ │ │ ├── ASTParserTest.java │ │ │ └── IScannerTest.java │ │ │ ├── dagger │ │ │ ├── annotation │ │ │ │ └── MyAnnotation.java │ │ │ ├── CircleBindingTest.java │ │ │ └── DaggerTest.java │ │ │ ├── concurrent │ │ │ ├── ThreadWithStop.java │ │ │ ├── SemaphorePrint.java │ │ │ ├── CountDownLatchThread.java │ │ │ ├── ThreadWait.java │ │ │ ├── ExceptionHandler.java │ │ │ ├── ProducerAndComsumer.java │ │ │ ├── PhaserPrint.java │ │ │ ├── CyclicBarrierPrint.java │ │ │ ├── SynchronizedMethod.java │ │ │ └── Basic.java │ │ │ ├── springboot │ │ │ ├── ConditionalOnMyProperties.java │ │ │ ├── MyEnableAutoConfiguration.java │ │ │ ├── OnMyPropertiesCondition.java │ │ │ ├── MyEnableAutoConfigurationImport.java │ │ │ ├── CustomizeEnableAutoConfigure.java │ │ │ ├── Bootstrap.java │ │ │ ├── CustomizePropertySourceLoader.java │ │ │ └── CustomizeConditional.java │ │ │ └── guice │ │ │ ├── SetBinderTest.java │ │ │ ├── TwoModuleTest.java │ │ │ ├── MapBinderTest.java │ │ │ └── GuiceTest.java │ └── resources │ │ ├── application.json │ │ ├── META-INF │ │ └── spring.factories │ │ └── logback.xml └── test │ └── java │ └── org │ └── wcong │ └── test │ ├── package-info.java │ ├── leetcode │ ├── package-info.java │ ├── string │ │ ├── ShortestPalindromeTest.java │ │ ├── SubstringWithConcatenationAllWordsTest.java │ │ ├── InterleavingStringTest.java │ │ ├── WordSearch2Test.java │ │ ├── KthSmallestLexicographicalOrderTest.java │ │ ├── ReversePiarsTest.java │ │ ├── WordBreak2Test.java │ │ └── LongestValidParenthesesTest.java │ ├── array │ │ ├── MedianOfTwoSortedArrayTest.java │ │ ├── CandyTest.java │ │ ├── NUmbersOfIslandsTest.java │ │ ├── BestTimeBuySellStockIVTest.java │ │ ├── CreateMaximumNumberTest.java │ │ └── MaxPointsOnALineTest.java │ ├── NumberToWordsTest.java │ └── tree │ │ └── BinaryTreeMaximumSubPathTest.java │ ├── hackerrank │ ├── package-info.java │ └── string │ │ ├── BearAndSteadyGeneTest.java │ │ └── PerfectStringTest.java │ └── concurrent │ ├── ThreadWithStopTest.java │ ├── SynchronizedMethodTest.java │ ├── PhaserPrintTest.java │ ├── CountDownLatchThreadTest.java │ ├── SemaphorePrintTest.java │ └── CyclicBarrierPrintTest.java ├── README.md └── annotation ├── src └── main │ ├── resources │ └── META-INF │ │ └── services │ │ └── javax.annotation.processing.Processor │ └── java │ └── org │ └── wcong │ └── test │ ├── mydagger │ ├── MyInject.java │ ├── MyComponent.java │ ├── MyProvides.java │ └── MyComponentProcessor.java │ ├── autovalue │ └── MyAutoValue.java │ └── util │ ├── ListUtils.java │ └── ElementUtils.java └── pom.xml /doc/dagger/MyDagger.md: -------------------------------------------------------------------------------- 1 | ### 前言 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .* 3 | target/ 4 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/tomcat/welcome.txt: -------------------------------------------------------------------------------- 1 | welcome -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test; -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/package-info.java: -------------------------------------------------------------------------------- 1 | //for leetcode -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode; -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/hackerrank/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.hackerrank; -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/hackerrank/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.hackerrank; -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/rxjava/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.rxjava; 2 | 3 | // about rxjava -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/pattern/package-info.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.pattern; 2 | // about java pattern examples -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## learn-java 2 | example of learn java 3 | 4 | algorithm has moved to [learn-algorithm](https://github.com/wcong/learn-algorithm) 5 | 6 | 7 | -------------------------------------------------------------------------------- /annotation/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | org.wcong.test.autovalue.MyAutoValueProcessor 2 | org.wcong.test.mydagger.MyComponentProcessor -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/datastructure/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * most data structure i should know 3 | * Created by wcong on 2017/4/5. 4 | */ 5 | package org.wcong.test.datastructure; -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/mydagger/MyProvidesTest1.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | /** 4 | * Created by wcong on 2016/12/6. 5 | */ 6 | @MyProvides 7 | public class MyProvidesTest1 { 8 | } 9 | -------------------------------------------------------------------------------- /doc/transaction/BasicClass.md: -------------------------------------------------------------------------------- 1 | TransactionAnnotationParser 2 | TransactionAttribute TransactionDefinition 3 | TransactionAttributeSource 4 | TransactionInterceptor 5 | TransactionStatus 6 | PlatformTransactionManager -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/mydagger/MyInject.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2016/12/4 6 | */ 7 | public class MyInject { 8 | } 9 | -------------------------------------------------------------------------------- /doc/aop/Aop.md: -------------------------------------------------------------------------------- 1 | #### Entrance 2 | 3 | * @EnableAspectJAutoProxy 4 | * @Import import class 5 | * AnnotationAwareAspectJAutoProxyCreator(order int max to deal last) 6 | * AnnotationAwareAspectJAutoProxyCreator parse aop class -------------------------------------------------------------------------------- /doc/bean/BasicClass.md: -------------------------------------------------------------------------------- 1 | ### BeanDefinitionRegistry 2 | 3 | register bean definition 4 | 5 | ### BeanFactory 6 | 7 | class for create bean 8 | 9 | ### FactoryBean 10 | 11 | actually create bean ,hold by BeanFactory 12 | -------------------------------------------------------------------------------- /src/main/resources/application.json: -------------------------------------------------------------------------------- 1 | { 2 | "customize": { 3 | "property": { 4 | "message": "hello world" 5 | } 6 | }, 7 | "logging": { 8 | "level": { 9 | "root": "INFO" 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /doc/bean/IntegrateBeanFactory.md: -------------------------------------------------------------------------------- 1 | ### BeanFactory after initial 2 | 3 | BeanFactoryPostProcessor 4 | BeanDefinitionRegistryPostProcessor: for customer create bean 5 | 6 | ### Bean before and after 7 | 8 | BeanPostProcessor 9 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/RBTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | /** 4 | * Created by hzwangcong on 2017/2/6. 5 | */ 6 | public class RBTree { 7 | public static void main(String[] args) { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/AllPermutation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give a string of unique character ,return all permutation 5 | * Created by wcong on 2017/3/31. 6 | */ 7 | public class AllPermutation { 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/dp_divide_conque/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.dp_divide_conque; 2 | 3 | /** 4 | * @author wcong 5 | * @since 19/04/2017 6 | */ 7 | public class TowerOfHanoi { 8 | } 9 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.env.PropertySourceLoader=org.wcong.test.springboot.JsonPropertySourceLoader 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.wcong.test.springboot.CustomizeEndPoint.EndPointAutoConfig -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/graph/TopologicalSort.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.graph; 2 | 3 | /** 4 | * topological sort 5 | * 6 | * @author wcong 7 | * @since 2017/4/9 8 | */ 9 | public class TopologicalSort { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/Tree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class Tree { 8 | 9 | public TreeNode root; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/mydagger/MyComponentTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | /** 4 | * Created by wcong on 2016/12/7. 5 | */ 6 | @MyComponent 7 | public interface MyComponentTest { 8 | 9 | MyProvidesTest myProvidesTest(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /doc/bean/BeanFactory.md: -------------------------------------------------------------------------------- 1 | ### singletons init 2 | ApplicationContext.finishBeanFactoryInitialization init all left singletons 3 | 4 | 5 | ### creat bean 6 | 7 | AbstractAutowireCapableBeanFactory.populateBean autowired field 8 | AbstractAutowireCapableBeanFactory.doCreateBean create bean -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/aop/Proceed.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.aop; 2 | 3 | /** 4 | * @author wcong 5 | * @since 16/4/18 6 | */ 7 | public interface Proceed { 8 | Object proceed(MethodInvocation methodInvocation) throws Throwable; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/dp_divide_conque/TripleStep.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.dp_divide_conque; 2 | 3 | /** 4 | * 5 | * @author wcong 6 | * @since 19/04/2017 7 | */ 8 | public class TripleStep { 9 | 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/DubboLinkedInt.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | /** 4 | * Created by hzwangcong on 2017/2/7. 5 | */ 6 | public class DubboLinkedInt { 7 | 8 | int value; 9 | 10 | DubboLinkedInt left; 11 | 12 | DubboLinkedInt right; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /doc/bean/CustomizeBeanScan.md: -------------------------------------------------------------------------------- 1 | #### Further Than Customize Annotation 2 | 3 | [Full Example](../../src/main/java/org/wcong/test/spring/test/CustomizeScanTest.java) 4 | 5 | 6 | #### Basic Component Of Bean 7 | 8 | * Bean Factory: container for beans 9 | * Factory Bean: custimize create beans 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/ThreeSumClosed.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | /** 4 | * dasdsaadsaasdsasadsdss 5 | * Created by hzwangcong on 2017/3/9. 6 | */ 7 | public class ThreeSumClosed { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/TreeNode.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | /** 4 | * Created by wcong on 2017/4/1. 5 | */ 6 | public class TreeNode { 7 | 8 | public int val; 9 | 10 | public TreeNode left; 11 | 12 | public TreeNode right; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /doc/bean/HoldAllBean.md: -------------------------------------------------------------------------------- 1 | ### class hold all bean 2 | 3 | DefaultListableBeanFactory 4 | 5 | ### field hold all bean and bean name 6 | 7 | private final Map beanDefinitionMap = new ConcurrentHashMap(64); 8 | 9 | private final List beanDefinitionNames = new ArrayList(); -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/linked_list/SumList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.linked_list; 2 | 3 | /** 4 | * @author wcong 5 | * @since 17/04/2017 6 | */ 7 | public class SumList { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/MyAutoValueClassTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | /** 4 | * Created by hzwangcong on 2016/11/25. 5 | */ 6 | @MyAutoValue 7 | public class MyAutoValueClassTest { 8 | 9 | private String a; 10 | 11 | private String b; 12 | 13 | private int c; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/scan/ScanClass1.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.scan; 2 | 3 | /** 4 | * @author wcong 5 | * @since 16/1/22 6 | */ 7 | @CustomizeComponent 8 | public class ScanClass1 { 9 | 10 | public void print() { 11 | System.out.println("scanClass1"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/TreeNode.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class TreeNode { 8 | 9 | public int value; 10 | 11 | public TreeNode left; 12 | 13 | public TreeNode right; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/jpa/DbStockRepository.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.jpa; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | /** 6 | * @author wcong 7 | * @since 16/4/20 8 | */ 9 | public interface DbStockRepository extends CrudRepository { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/AutoValueTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | /** 4 | * Created by wcong on 2016/11/22. 5 | */ 6 | public class AutoValueTest { 7 | public static void main(String[] args) { 8 | MyClass myClass = MyClass.create("world"); 9 | System.out.println(myClass); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/LongPrefix.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | /** 4 | * Write a function to find the longest common prefix string amongst an array of strings. 5 | * Created by hzwangcong on 2017/3/1. 6 | */ 7 | public class LongPrefix { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/graph/Vertices.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.graph; 2 | 3 | /** 4 | * Created by wcong on 2017/2/23. 5 | */ 6 | public class Vertices { 7 | 8 | private T t; 9 | 10 | 11 | public T getT() { 12 | return t; 13 | } 14 | 15 | public void setT(T t) { 16 | this.t = t; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /doc/menu/Menu.md: -------------------------------------------------------------------------------- 1 | ### menu 2 | 3 | - bean 4 | - [customize bean annotation](../bean/CustomizeBeanAnnotation.md) 5 | - [customize bean scan](../bean/CustomizeBeanScan.md) 6 | - bean post process 7 | - bean factory post process 8 | - context 9 | - application listener 10 | - aop 11 | - mvc 12 | - RequestMapping 13 | - tomcat 14 | - embed 15 | - listener 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/mydagger/MyProvidesTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | import javax.inject.Inject; 4 | 5 | /** 6 | * @author wcong 7 | * @since 2016/12/4 8 | */ 9 | @MyProvides 10 | public class MyProvidesTest { 11 | 12 | @Inject 13 | public MyProvidesTest(MyProvidesTest1 myProvidesTest1) { 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/MyClass.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | import com.google.auto.value.AutoValue; 4 | 5 | /** 6 | * Created by wcong on 2016/11/22. 7 | */ 8 | @AutoValue 9 | abstract class MyClass { 10 | static MyClass create(String hello) { 11 | return new AutoValue_MyClass(hello); 12 | } 13 | 14 | abstract String hello(); 15 | } 16 | -------------------------------------------------------------------------------- /doc/bean/ResolveDependency.md: -------------------------------------------------------------------------------- 1 | ### how spring resolve dependency for a bean 2 | 3 | ``` java 4 | org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency() 5 | ``` 6 | 7 | #### detail 8 | judge type in (String,array,Collection,Map,etc) 9 | 10 | #### Collection 11 | 12 | usage of generic type reflection 13 | 14 | ``` java 15 | ParameterizedType 16 | ``` 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/scan/CustomizeComponent.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.scan; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * @author wcong 7 | * @since 16/1/22 8 | */ 9 | @Target({ ElementType.TYPE }) 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Documented 12 | public @interface CustomizeComponent { 13 | String value() default ""; 14 | } 15 | -------------------------------------------------------------------------------- /doc/springboot/import.md: -------------------------------------------------------------------------------- 1 | 2 | *ConditionEvaluator 3 | *ConfigurationClassParser 4 | *ConfigurationClassPostProcessor 5 | *ConfigurationClassBeanDefinitionReader 6 | *ConfigurationPropertiesBindingPostProcessorRegistrar 7 | *BeanDefinitionBuilder 8 | *ImportRegistry 9 | 10 | 11 | *PropertySourcesLoader 12 | *ConfigFileApplicationListener 13 | 14 | *EndpointMvcAdapter 15 | *MvcEndpoints 16 | *EndpointMvcAdapter -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/LinkedListNode.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class LinkedListNode { 8 | 9 | public int value; 10 | 11 | public LinkedListNode next; 12 | 13 | public String toString() { 14 | return String.valueOf(value); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/ComplexListNode.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class ComplexListNode { 8 | public int value; 9 | 10 | public ComplexListNode next; 11 | 12 | public ComplexListNode other; 13 | 14 | public String toString() { 15 | return String.valueOf(value); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/FullInject.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Target(ElementType.TYPE) 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Documented 12 | public @interface FullInject { 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyInject.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Target(ElementType.FIELD) 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Documented 12 | public @interface MyInject { 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/MyAnnotationImpl.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | import java.lang.annotation.Annotation; 4 | 5 | /** 6 | * Created by hzwangcong on 2016/11/22. 7 | */ 8 | public class MyAnnotationImpl implements MyAnnotation { 9 | public String value() { 10 | return null; 11 | } 12 | 13 | public Class annotationType() { 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/basic/LinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.basic; 2 | 3 | /** 4 | * @author wcong 5 | * @since 17/04/2017 6 | */ 7 | public class LinkedList { 8 | 9 | public Node root; 10 | 11 | public static class Node { 12 | 13 | public int value; 14 | 15 | public Node next; 16 | 17 | public String toString() { 18 | return String.valueOf(value); 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/javaformat/FormatTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.javaformat; 2 | 3 | import com.google.googlejavaformat.java.Main; 4 | 5 | /** 6 | * Created by wcong on 2016/11/17. 7 | */ 8 | public class FormatTest { 9 | 10 | public static void main(String[] args) { 11 | String[] files = new String[1]; 12 | files[0] = "src/main/java/org/wcong/test/guice/GuiceTest.java"; 13 | Main.main(files); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyComponent.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * @author wcong 9 | * @since 16/1/21 10 | */ 11 | @Target({ ElementType.TYPE }) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | @Documented 14 | @Component 15 | public @interface MyComponent { 16 | 17 | String value() default ""; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/StringMultiply.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | /** 4 | * give two string of integer, calculate the product of them 5 | * Created by wcong on 2017/4/1. 6 | */ 7 | public class StringMultiply { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | public static String multiply(String a, String b) { 14 | return null; 15 | } 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/autovalue/MyAutoValue.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * my auto value 10 | * Created by wcong on 2016/11/25. 11 | */ 12 | @Retention(RetentionPolicy.SOURCE) 13 | @Target(ElementType.TYPE) 14 | public @interface MyAutoValue { 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/MyAnnotation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by hzwangcong on 2016/11/22. 10 | */ 11 | @Target(ElementType.METHOD) 12 | @Retention(RetentionPolicy.SOURCE) 13 | public @interface MyAnnotation { 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/javaformat/RangeTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.javaformat; 2 | 3 | import com.google.common.collect.Range; 4 | 5 | /** 6 | * Created by wcong on 2016/11/21. 7 | */ 8 | public class RangeTest { 9 | 10 | public static void main(String[] args) { 11 | Range stringRange = Range.open("a", "b"); 12 | System.out.println(stringRange); 13 | System.out.println(stringRange.span(Range.open("c", "d"))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/mydagger/MyComponent.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * @author wcong 10 | * @since 2016/12/4 11 | */ 12 | @Retention(RetentionPolicy.SOURCE) 13 | @Target(ElementType.TYPE) 14 | public @interface MyComponent { 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyBefore.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * before method 10 | * 11 | * @author wcong 12 | * @since 16/4/13 13 | */ 14 | @Target({ ElementType.TYPE }) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | public @interface MyBefore { 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyRequestBody.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Created by wcong on 2016/5/6. 11 | */ 12 | @Target(ElementType.PARAMETER) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Documented 15 | public @interface MyRequestBody { 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/array/TwoSum.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.array; 2 | 3 | /** 4 | * give a array of integer and a target,return the indices where two sum equals the target 5 | * assume only one solution 6 | * Created by wcong on 2017/4/5. 7 | */ 8 | public class TwoSum { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | public static int[] indices(int[] array, int target) { 15 | return null; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyResponseBody.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Created by wcong on 2016/5/6. 11 | */ 12 | @Target({ElementType.TYPE, ElementType.METHOD}) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Documented 15 | public @interface MyResponseBody { 16 | } 17 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/mydagger/MyProvides.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * my provides 10 | * 11 | * @author wcong 12 | * @since 2016/12/4 13 | */ 14 | @Retention(RetentionPolicy.SOURCE) 15 | @Target({ ElementType.METHOD, ElementType.TYPE }) 16 | public @interface MyProvides { 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/linked_list/PrintLinkedListFromBottom.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.linked_list; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * test for linked list 7 | * give a linked list pring it from bottom 8 | * Created by wcong on 2017/3/23. 9 | */ 10 | public class PrintLinkedListFromBottom { 11 | 12 | public static void main(String[] args) { 13 | 14 | } 15 | 16 | public static void printRecursive(List linkedList) { 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/hackerrank/string/BearAndSteadyGeneTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.hackerrank.string; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author wcong 8 | * @since 04/05/2017 9 | */ 10 | public class BearAndSteadyGeneTest { 11 | 12 | @Test 13 | public void test() { 14 | BearAndSteadyGene bearAndSteadyGene = new BearAndSteadyGene(); 15 | Assert.isTrue(bearAndSteadyGene.minimumReplace("GAAATAAA") == 5); 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/array_and_string/CheckPermutation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.array_and_string; 2 | 3 | /** 4 | * give two string write a method to decide if one is permutation of the other 5 | * 6 | * @author wcong 7 | * @since 17/04/2017 8 | */ 9 | public class CheckPermutation { 10 | 11 | public static void main(String[] args) { 12 | 13 | } 14 | 15 | public static boolean checkPermutation(String a, String b) { 16 | return false; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/dagger/annotation/MyAnnotation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.dagger.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * @author wcong 11 | * @since 2016/11/4 12 | */ 13 | @Retention(RetentionPolicy.SOURCE) 14 | @Documented 15 | @Target(ElementType.TYPE) 16 | public @interface MyAnnotation { 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Created by wcong on 2016/5/16. 11 | */ 12 | @Target(ElementType.METHOD) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Documented 15 | public @interface MyExceptionHandler { 16 | Class[] value() default {}; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/aop/MyAspect.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.aop; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | @Target({ ElementType.TYPE }) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | @Documented 14 | @Component 15 | public @interface MyAspect { 16 | String value() default ""; 17 | 18 | String pointCut(); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/graph/Edge.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.graph; 2 | 3 | /** 4 | * Created by wcong on 2017/2/23. 5 | */ 6 | public class Edge { 7 | 8 | private Vertices start; 9 | 10 | private Vertices end; 11 | 12 | public Vertices getStart() { 13 | return start; 14 | } 15 | 16 | public void setStart(Vertices start) { 17 | this.start = start; 18 | } 19 | 20 | public Vertices getEnd() { 21 | return end; 22 | } 23 | 24 | public void setEnd(Vertices end) { 25 | this.end = end; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/array_and_string/URLify.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.array_and_string; 2 | 3 | /** 4 | * Write a method to replace all spaces in a string with '%20 5 | * You may assume that the string has sufficient space at the end to hold the additional characters 6 | * and that you are given the "true" length of the string. 7 | * (Note: If implementing in Java,please use a character array so that you can perform this operation in place.) 8 | * @author wcong 9 | * @since 17/04/2017 10 | */ 11 | public class URLify { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/AutoAnnotationTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | import com.google.auto.value.AutoAnnotation; 4 | 5 | /** 6 | * Created by hzwangcong on 2016/11/22. 7 | */ 8 | public class AutoAnnotationTest { 9 | 10 | public static void main(String[] args) { 11 | MyAnnotation myAnnotation = new MyAnnotationImpl(); 12 | System.out.println(myAnnotation("hello")); 13 | } 14 | 15 | @AutoAnnotation 16 | public static MyAnnotation myAnnotation(String value) { 17 | return new AutoAnnotation_AutoAnnotationTest_myAnnotation(value); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/ThreadWithStop.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | /** 4 | * @author wcong 5 | * @since 30/04/2017 6 | */ 7 | public class ThreadWithStop extends Thread { 8 | 9 | private boolean stop; 10 | 11 | public void setStop(boolean stop) { 12 | this.stop = stop; 13 | } 14 | 15 | public void run() { 16 | while (!stop) { 17 | try { 18 | sleep(1000L); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | System.out.println("I'm running"); 23 | } 24 | System.out.println("I stopped"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyControllerAdvice.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * Created by wcong on 2016/5/16. 13 | */ 14 | @Target({ElementType.TYPE}) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | @Component 18 | public @interface MyControllerAdvice { 19 | String[] value() default {}; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyController.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * @author wcong 13 | * @since 16/4/28 14 | */ 15 | @Target({ ElementType.METHOD, ElementType.TYPE }) 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Documented 18 | @Component 19 | public @interface MyController { 20 | String value() default ""; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/autovalue/MyAutoValueTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.autovalue; 2 | 3 | /** 4 | * Created by wcong on 2016/11/26. 5 | */ 6 | public class MyAutoValueTest { 7 | public static void main(String[] args) { 8 | org.wcong.test.autovalue.MyAutoValueClassTest_MyAutoValue myAutoValueClassTest_myAutoValue = new org.wcong.test.autovalue.MyAutoValueClassTest_MyAutoValue(); 9 | myAutoValueClassTest_myAutoValue.setA("a"); 10 | myAutoValueClassTest_myAutoValue.setB("b"); 11 | myAutoValueClassTest_myAutoValue.setC(1); 12 | System.out.println(myAutoValueClassTest_myAutoValue.toString()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/graph/BuildOrder.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.graph; 2 | 3 | /** 4 | * give a list of project and a list of dependencies(where a list of pairs of project where the second project dependent on the first project) 5 | * all the project's dependencies must be built for the project is 6 | * find a build order that will build order that allow the projects to be built 7 | * if there is no valid order ,return an error 8 | * 9 | * @author wcong 10 | * @since 18/04/2017 11 | */ 12 | public class BuildOrder { 13 | 14 | public static void main(String[] args) { 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/hackerrank/string/PerfectStringTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.hackerrank.string; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author wcong 8 | * @since 03/05/2017 9 | */ 10 | public class PerfectStringTest { 11 | 12 | @Test 13 | public void test() { 14 | PerfectString perfectString = new PerfectString(); 15 | Assert.isTrue(perfectString.perfectNum("abcd") == 3); 16 | Assert.isTrue(perfectString.perfectNum("abc") == 1); 17 | Assert.isTrue(perfectString.perfectNum("ddc") == 2); 18 | Assert.isTrue(perfectString.perfectNum("dddccc") == 19); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/ConditionalOnMyProperties.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.context.annotation.Conditional; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * Created by wcong on 2016/6/13. 13 | */ 14 | @Target({ElementType.TYPE, ElementType.METHOD}) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | @Conditional(OnMyPropertiesCondition.class) 18 | public @interface ConditionalOnMyProperties { 19 | String name(); 20 | } 21 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/util/ListUtils.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.util; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by wcong on 2016/12/6. 8 | */ 9 | public class ListUtils { 10 | 11 | public static String implode(List list, String joint) { 12 | if (list == null || list.isEmpty()) { 13 | return ""; 14 | } 15 | Iterator iterator = list.iterator(); 16 | StringBuilder sb = new StringBuilder(); 17 | sb.append(iterator.next()); 18 | while (iterator.hasNext()) { 19 | sb.append(joint).append(iterator.next()); 20 | } 21 | return sb.toString(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/BuildLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class BuildLinkedList { 8 | 9 | public static LinkedList makeOne(int[] array) { 10 | LinkedListNode root = new LinkedListNode(); 11 | LinkedList linkedList = new LinkedList(); 12 | linkedList.root = root; 13 | root.value = array[0]; 14 | LinkedListNode parent = root; 15 | for (int i = 1; i < array.length; i++) { 16 | LinkedListNode node = new LinkedListNode(); 17 | node.value = array[i]; 18 | parent.next = node; 19 | parent = node; 20 | } 21 | return linkedList; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/SemaphorePrint.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.Semaphore; 4 | 5 | /** 6 | * @author wcong 7 | * @since 30/04/2017 8 | */ 9 | public class SemaphorePrint { 10 | 11 | private Semaphore semaphore = new Semaphore(2); 12 | 13 | public void print() { 14 | try { 15 | semaphore.acquire(); 16 | System.out.println("acquire the semaphore :" + Thread.currentThread().getId() + ";"); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } finally { 20 | System.out.println("release the semaphore :" + Thread.currentThread().getId() + ";"); 21 | semaphore.release(); 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/dp_divide_conque/BooleanEvaluation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.dp_divide_conque; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * given a boolean evaluation consisting of symbol 7 | * 0(false),1(true),&(and),|(OR),and ^(XOR) and a desired boolean result Result 8 | * implement a function to count the number of ways of parenthesizing the expressions 9 | * such that it evaluates to result. 10 | * the expression should be fully parenthesized 11 | * 12 | * @author wcong 13 | * @since 19/04/2017 14 | */ 15 | public class BooleanEvaluation { 16 | 17 | public int count(String expression, boolean result) { 18 | return 0; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/ValidateBinaryTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | /** 4 | * give a binary tree,test if it is a binary tree 5 | * Created by wcong on 2017/4/1. 6 | */ 7 | public class ValidateBinaryTree { 8 | 9 | public static void main(String args) { 10 | } 11 | 12 | static int max = Integer.MIN_VALUE; 13 | 14 | public static boolean validate(TreeNode treeNode) { 15 | if (treeNode == null) { 16 | return true; 17 | } 18 | if (!validate(treeNode.left)) { 19 | return false; 20 | } 21 | if (treeNode.val >= max) { 22 | max = treeNode.val; 23 | } else { 24 | return false; 25 | } 26 | return validate(treeNode.right); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/ShortestPalindromeTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.ShortestPalindrome; 6 | 7 | /** 8 | * @author wcong 9 | * @since 07/06/2017 10 | */ 11 | public class ShortestPalindromeTest { 12 | 13 | @Test 14 | public void testShortestPalindrome() { 15 | ShortestPalindrome solution = new ShortestPalindrome(); 16 | Assert.assertTrue(solution.shortestPalindromeBruteForce("aacecaaa").equals("aaacecaaa")); 17 | Assert.assertTrue(solution.shortestPalindromeBruteForce("abcd").equals("dcbabcd")); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/CountDownLatchThread.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | /** 6 | * @author wcong 7 | * @since 30/04/2017 8 | */ 9 | public class CountDownLatchThread implements Runnable { 10 | 11 | private CountDownLatch countDownLatch; 12 | 13 | public CountDownLatchThread(CountDownLatch countDownLatch) { 14 | this.countDownLatch = countDownLatch; 15 | } 16 | 17 | public void run() { 18 | System.out.println("wait for it"); 19 | countDownLatch.countDown(); 20 | try { 21 | countDownLatch.await(); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | System.out.println("start"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/CollectionsUtilTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.apache.commons.collections.CollectionUtils; 4 | import org.apache.commons.collections.functors.NotNullPredicate; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * @author wcong 11 | * @since 15/12/9 12 | */ 13 | public class CollectionsUtilTest { 14 | 15 | public static void main(String[] args) { 16 | List test = new ArrayList(); 17 | test.add(0); 18 | test.add(null); 19 | test.add(2); 20 | test.add(10); 21 | System.out.println(test); 22 | CollectionUtils.filter(test, NotNullPredicate.getInstance()); 23 | System.out.println(test); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/annotation/DefaultAnnotationNameTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.annotation; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * Created by wcong on 2016/6/16. 8 | */ 9 | @Configuration 10 | public class DefaultAnnotationNameTest { 11 | 12 | public static void main(String[] args) { 13 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 14 | annotationConfigApplicationContext.register(DefaultAnnotationNameTest.class); 15 | annotationConfigApplicationContext.refresh(); 16 | } 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/mybatis/mapper/DbTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.mybatis.mapper; 2 | 3 | import org.apache.ibatis.annotations.Insert; 4 | import org.apache.ibatis.annotations.Select; 5 | import org.wcong.test.spring.MyTransactional; 6 | import org.wcong.test.spring.mybatis.model.TestModel; 7 | 8 | /** 9 | * @author wcong 10 | * @since 16/4/21 11 | */ 12 | public interface DbTest { 13 | 14 | @Select("select count(*) from db_test") 15 | int count(); 16 | 17 | @Insert("insert into db_test(id,date,content) values(#{id},now(),#{content})") 18 | @MyTransactional 19 | int add(TestModel testModel); 20 | 21 | @Insert("create table db_test(id int,date time,content varchar)") 22 | int createTable(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/ThreadWithStopTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * @author wcong 9 | * @since 30/04/2017 10 | */ 11 | public class ThreadWithStopTest { 12 | 13 | @Test 14 | public void testStop() { 15 | ThreadWithStop thread = new ThreadWithStop(); 16 | thread.start(); 17 | try { 18 | TimeUnit.MILLISECONDS.sleep(5000L); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | System.out.println("i will set you stop"); 23 | thread.setStop(true); 24 | try { 25 | thread.join(); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/SubstringWithConcatenationAllWordsTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.SubstringWithConcatenationAllWords; 6 | 7 | import java.util.Arrays; 8 | 9 | /** 10 | * @author wcong 11 | * @since 17/05/2017 12 | */ 13 | public class SubstringWithConcatenationAllWordsTest { 14 | 15 | @Test 16 | public void testFindSubstring() { 17 | SubstringWithConcatenationAllWords test = new SubstringWithConcatenationAllWords(); 18 | Assert.assertTrue(test.findSubstringBruteForce("barfoothefoobarman", new String[] { "foo", "bar" }) 19 | .containsAll(Arrays.asList(0, 9))); 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/MedianOfTwoSortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.MedianOfTwoSortedArray; 6 | 7 | /** 8 | * @author wcong 9 | * @since 11/05/2017 10 | */ 11 | public class MedianOfTwoSortedArrayTest { 12 | 13 | @Test 14 | public void testFindMedianSortedArrays() { 15 | MedianOfTwoSortedArray medianOfTwoSortedArray = new MedianOfTwoSortedArray(); 16 | Assert.assertTrue(medianOfTwoSortedArray.findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 2 }) == 2); 17 | Assert.assertTrue(medianOfTwoSortedArray.findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 3, 4 }) == 2.5); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/MyEnableAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.boot.autoconfigure.AutoConfigurationPackage; 4 | import org.springframework.context.annotation.Import; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Inherited; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * Created by wcong on 2016/5/31. 15 | */ 16 | @Target(ElementType.TYPE) 17 | @Retention(RetentionPolicy.RUNTIME) 18 | @Documented 19 | @Inherited 20 | @AutoConfigurationPackage 21 | @Import(MyEnableAutoConfigurationImport.class) 22 | public @interface MyEnableAutoConfiguration { 23 | } 24 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %date [%level] %logger{10} [%file : %line] %M %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/SynchronizedMethodTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * @author wcong 7 | * @since 30/04/2017 8 | */ 9 | public class SynchronizedMethodTest { 10 | 11 | @Test 12 | public void test() { 13 | SynchronizedMethod synchronizedMethod = new SynchronizedMethod(); 14 | SynchronizedMethod.MyPutThread myPutThread = new SynchronizedMethod.MyPutThread(synchronizedMethod); 15 | myPutThread.start(); 16 | SynchronizedMethod.MyGetThread myGetThread = new SynchronizedMethod.MyGetThread(synchronizedMethod); 17 | myGetThread.start(); 18 | try { 19 | myGetThread.join(); 20 | myPutThread.join(); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/CandyTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.Candy; 6 | 7 | /** 8 | * @author wcong 9 | * @since 19/06/2017 10 | */ 11 | public class CandyTest { 12 | 13 | @Test 14 | public void testCandies() { 15 | Candy candy = new Candy(); 16 | Assert.assertTrue(candy.candy(new int[]{11111}) == 1); 17 | Assert.assertTrue(candy.candy(new int[]{1, 2, 2}) == 4); 18 | Assert.assertTrue(candy.candy(new int[]{5, 4, 3, 2, 1, 0}) == 21); 19 | Assert.assertTrue(candy.candy(new int[]{5, 4, 4, 2, 1, 0}) == 13); 20 | Assert.assertTrue(candy.candy(new int[]{4, 2, 3, 4, 1}) == 9); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/InterleavingStringTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.InterleavingString; 6 | 7 | /** 8 | * @author wcong 9 | * @since 15/06/2017 10 | */ 11 | public class InterleavingStringTest { 12 | 13 | @Test 14 | public void testIsInterleave() { 15 | String s1 = "aabcc"; 16 | String s2 = "dbbca"; 17 | String s3 = "aadbbcbcac"; 18 | String s4 = "aadbbbaccc"; 19 | InterleavingString solution = new InterleavingString(); 20 | Assert.assertTrue(solution.isInterleaveBruteForce(s1, s2, s3)); 21 | Assert.assertTrue(!solution.isInterleaveBruteForce(s1, s2, s4)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/ChangeMaking.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give change for amount n using the minimum number of coin of denomination d1 7 | * @since 2017/4/9 8 | */ 9 | public class ChangeMaking { 10 | 11 | public static void main(String[] args) { 12 | 13 | } 14 | 15 | public static int minimumNums(int[] array, int amount) { 16 | int[] result = new int[amount + 1]; 17 | result[0] = 0; 18 | for (int i = 1; i <= amount; i++) { 19 | int min = i; 20 | for (int j = 0; j < array.length; j++) { 21 | if (array[j] > i) { 22 | break; 23 | } 24 | min = Math.min(min, result[i - array[j]] + 1); 25 | } 26 | result[i] = min; 27 | } 28 | return result[amount]; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/array_and_string/PalindromePermutation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.array_and_string; 2 | 3 | /** 4 | * give a string write a function to check if it is a permutation of a palindrome 5 | * a palindrome is a word phrase that has same forward and backward 6 | * a permutation is a rearrangement of letters 7 | * the palindrome does not need to be limit to just dictionary word 8 | * 9 | * key only one odd word 10 | * 1. hash map count the appearance 11 | * 2. binary operate, all exclusive or find the left num,second time,exclusive or other the last result 12 | * @author wcong 13 | * @since 17/04/2017 14 | */ 15 | public class PalindromePermutation { 16 | 17 | public static void main(String[] args){ 18 | int b = ~1; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/NumberToWordsTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.numbers.NumberToWords; 6 | 7 | /** 8 | * @author wcong 9 | * @since 16/05/2017 10 | */ 11 | public class NumberToWordsTest { 12 | 13 | @Test 14 | public void testNumberToWords() { 15 | NumberToWords numberToWords = new NumberToWords(); 16 | Assert.assertTrue(numberToWords.numberToWords(123).equals("One Hundred Twenty Three")); 17 | Assert.assertTrue(numberToWords.numberToWords(12345).equals("Twelve Thousand Three Hundred Forty Five")); 18 | Assert.assertTrue(numberToWords.numberToWords(1234567) 19 | .equals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven")); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/ThreadWait.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** 6 | * @author wcong 7 | * @since 29/04/2017 8 | */ 9 | public class ThreadWait { 10 | 11 | public static class MyThread extends Thread { 12 | public MyThread(String name) { 13 | setName(name); 14 | } 15 | 16 | public void run() { 17 | TimeUnit.MILLISECONDS.toSeconds(1000); 18 | System.out.println(getName()); 19 | } 20 | } 21 | 22 | public static void main(String[] args) throws InterruptedException { 23 | MyThread thread1 = new MyThread("1"); 24 | MyThread thread2 = new MyThread("2"); 25 | thread1.start(); 26 | thread2.start(); 27 | thread1.join(); 28 | thread2.join(); 29 | System.out.println("finished"); 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | /** 4 | * @author wcong 5 | * @since 29/04/2017 6 | */ 7 | public class ExceptionHandler { 8 | 9 | public static class MyThread extends Thread { 10 | public void run() { 11 | throw new RuntimeException("hello exception"); 12 | } 13 | } 14 | 15 | public static class MyExceptionHandler implements Thread.UncaughtExceptionHandler { 16 | 17 | @Override 18 | public void uncaughtException(Thread t, Throwable e) { 19 | System.out.println(e.toString()); 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | MyThread myThread = new MyThread(); 25 | myThread.setUncaughtExceptionHandler(new MyExceptionHandler()); 26 | myThread.start(); 27 | System.out.println("haha"); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/CoinRow.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * this is a row of n coins whose values are some positive integers c1,c2,....,cn 5 | * not necessarily distinct,the goal is to pick up the maximum amount of money subject 6 | * that no two coins adjacent in the initial row can be picked up. 7 | * 8 | * @author wcong 9 | * @since 2017/4/9 10 | */ 11 | public class CoinRow { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | public static int maxCoinSum(int[] array) { 18 | int[] result = new int[array.length]; 19 | result[0] = 0; 20 | result[1] = array[0]; 21 | for (int i = 1; i < array.length; i++) { 22 | result[i + 1] = Math.max(result[i], result[i - 1] + array[i]); 23 | } 24 | return result[array.length]; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/LinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class LinkedList { 8 | 9 | public LinkedListNode root; 10 | 11 | public boolean equal(LinkedList other) { 12 | if (other == null) { 13 | return false; 14 | } 15 | LinkedListNode otherNode = other.root; 16 | LinkedListNode thisNode = root; 17 | while (true) { 18 | if ((otherNode == null && thisNode == null) || (otherNode != null && thisNode != null 19 | && thisNode.value == otherNode.value)) { 20 | if (otherNode == null) { 21 | break; 22 | } 23 | otherNode = otherNode.next; 24 | thisNode = thisNode.next; 25 | } else { 26 | return false; 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/CoinsCollect.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give a m*n matrix board each cell is 0 cell or 1 cell, 5 | * a robot collect coin from left up 6 | * calculate the max coin number when he arrive the right bottom 7 | * 8 | * @author wcong 9 | * @since 2017/4/9 10 | */ 11 | public class CoinsCollect { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | public static int maxCoins(int[][] board) { 18 | int[][] result = new int[board.length + 1][board[0].length + 1]; 19 | for (int i = 0; i < board.length; i++) { 20 | for (int j = 0; j < board[0].length; j++) { 21 | result[i + 1][j + 1] = Math.max(result[i][j + 1], result[i + 1][j]) + board[i][j]; 22 | } 23 | } 24 | return result[board.length][board[0].length]; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/Successor.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking; 2 | 3 | import org.wcong.test.algorithm.tree.BinaryTree; 4 | 5 | /** 6 | * Created by wcong on 2017/4/14. 7 | */ 8 | public class Successor { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | public static BinaryTree.Node successor(BinaryTree.Node root, BinaryTree.Node target) { 15 | if (root == null || target == null) { 16 | return null; 17 | } 18 | BinaryTree.Node successor = null; 19 | while (root != null) { 20 | if (target.key < root.key) { 21 | successor = root; 22 | root = root.left; 23 | } else { 24 | root = root.right; 25 | } 26 | } 27 | return successor; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/dp/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.dp; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | 5 | /** 6 | * test for recursive and numbers 7 | * Created by wcong on 2017/3/24. 8 | */ 9 | public class Fibonacci { 10 | 11 | public static void main(String[] args) { 12 | Assert.isTrue(fibonacci(2) == 2); 13 | Assert.isTrue(fibonacci(4) == 5); 14 | Assert.isTrue(fibonacci(6) == 13); 15 | } 16 | 17 | static int fibonacci(int n) { 18 | if (n < 2) { 19 | return 1; 20 | } 21 | int last = 1; 22 | int lastOfLast = 1; 23 | for (int i = 2; i < n; i++) { 24 | int now = last + lastOfLast; 25 | lastOfLast = last; 26 | last = now; 27 | } 28 | return last + lastOfLast; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/Knapsack.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give n items has weight,w1,w2....,wn and values v1,v2....vn, 5 | * we have a bag contains m weight 6 | * find the largest value for the bag 7 | * 8 | * @author wcong 9 | * @since 2017/4/9 10 | */ 11 | public class Knapsack { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | public static int maxValue(int[] weight, int[] values, int bag) { 18 | int[] result = new int[bag + 1]; 19 | result[0] = 0; 20 | for (int i = 1; i <= bag; i++) { 21 | int max = Integer.MIN_VALUE; 22 | for (int j = 0; j < weight.length; j++) { 23 | if (i < weight[j]) { 24 | break; 25 | } 26 | max = Math.max(values[j] + result[i - weight[j]], max); 27 | } 28 | result[i] = max; 29 | } 30 | return result[bag]; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/stack/O1MinStack.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.stack; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * test for stack and its operation 7 | * implement a push pop and min all O(1) time use 8 | * 9 | * @author wcong 10 | * @since 2017/3/26 11 | */ 12 | public class O1MinStack { 13 | 14 | public static class MinO1Stack { 15 | 16 | private Stack normal; 17 | 18 | private Stack min; 19 | 20 | public void push(int num) { 21 | normal.push(num); 22 | if (min.isEmpty() || min.lastElement() > num) { 23 | min.push(num); 24 | } else { 25 | min.push(min.lastElement()); 26 | } 27 | } 28 | 29 | public int pop() { 30 | min.pop(); 31 | return normal.pop(); 32 | } 33 | 34 | public int min() { 35 | return min.lastElement(); 36 | } 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/tree/BinaryTreeMaximumSubPathTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.tree; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.tree.BinaryTreeMaximumSubPath; 6 | import org.wcong.test.algorithm.leetcode.tree.TreeNode; 7 | 8 | /** 9 | * @author wcong 10 | * @since 05/05/2017 11 | */ 12 | public class BinaryTreeMaximumSubPathTest { 13 | 14 | @Test 15 | public void test() { 16 | BinaryTreeMaximumSubPath maximumSubPath = new BinaryTreeMaximumSubPath(); 17 | TreeNode treeNode = new TreeNode(); 18 | treeNode.val = 1; 19 | TreeNode left = new TreeNode(); 20 | left.val = 2; 21 | treeNode.left = left; 22 | TreeNode right = new TreeNode(); 23 | right.val = 3; 24 | treeNode.right = right; 25 | Assert.assertTrue(maximumSubPath.maxPathSum(treeNode) == 6); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/pattern/creational/Multition.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.pattern.creational; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | 6 | /** 7 | * @author wcong 8 | * @since 2017/1/2 9 | */ 10 | public class Multition { 11 | 12 | public static void main(String[] args){ 13 | int[] nums = {1,3}; 14 | System.out.println(index(0,nums.length,nums,0)); 15 | } 16 | 17 | private static int index(int start,int end,int[] nums,int target){ 18 | if( start == end ){ 19 | if( nums[start] == target ){ 20 | return start; 21 | }else if( nums[start] > target ){ 22 | return start; 23 | }else{ 24 | return start +1; 25 | } 26 | } 27 | int middle = start + (end - start)/2; 28 | if( nums[middle] == target ){ 29 | return middle; 30 | } 31 | return index(start+1>middle?middle:start+1,end -1,nums,target); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/mybatis/model/TestModel.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.mybatis.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * @author wcong 7 | * @since 16/4/21 8 | */ 9 | public class TestModel { 10 | 11 | private Integer id; 12 | 13 | private Date date; 14 | 15 | private String content; 16 | 17 | public TestModel() { 18 | } 19 | 20 | public TestModel(String content) { 21 | this.content = content; 22 | } 23 | 24 | public Integer getId() { 25 | return id; 26 | } 27 | 28 | public void setId(Integer id) { 29 | this.id = id; 30 | } 31 | 32 | public Date getDate() { 33 | return date; 34 | } 35 | 36 | public void setDate(Date date) { 37 | this.date = date; 38 | } 39 | 40 | public String getContent() { 41 | return content; 42 | } 43 | 44 | public void setContent(String content) { 45 | this.content = content; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/javaformat/ASTParserTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.javaformat; 2 | 3 | import org.eclipse.jdt.core.dom.AST; 4 | import org.eclipse.jdt.core.dom.ASTParser; 5 | import org.eclipse.jdt.core.dom.CompilationUnit; 6 | 7 | import java.io.IOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.Path; 10 | import java.nio.file.Paths; 11 | 12 | /** 13 | * Created by wcong on 2016/11/21. 14 | */ 15 | public class ASTParserTest { 16 | public static void main(String[] args) throws IOException { 17 | ASTParser parser = ASTParser.newParser(AST.JLS8); 18 | Path path = Paths.get("src/main/java/org/wcong/test/guice/GuiceTest.java"); 19 | String text = new String(Files.readAllBytes(path)); 20 | parser.setSource(text.toCharArray()); 21 | CompilationUnit unit = (CompilationUnit) parser.createAST(null); 22 | unit.getJavaElement(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MyRequestMapping.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.web.bind.annotation.RequestMethod; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * @author wcong 13 | * @since 16/4/28 14 | */ 15 | @Target({ ElementType.METHOD, ElementType.TYPE }) 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Documented 18 | public @interface MyRequestMapping { 19 | 20 | String name() default ""; 21 | 22 | String[] value() default {}; 23 | 24 | RequestMethod[] method() default {}; 25 | 26 | String[] params() default {}; 27 | 28 | String[] headers() default {}; 29 | 30 | String[] consumes() default {}; 31 | 32 | String[] produces() default {}; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/graph/Graph.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.graph; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by wcong on 2017/2/23. 7 | */ 8 | public class Graph { 9 | 10 | private int vertices; 11 | 12 | private int edge; 13 | 14 | private List[] adjacencyList; 15 | 16 | 17 | public int getVertices() { 18 | return vertices; 19 | } 20 | 21 | public void setVertices(int vertices) { 22 | this.vertices = vertices; 23 | } 24 | 25 | public int getEdge() { 26 | return edge; 27 | } 28 | 29 | public void setEdge(int edge) { 30 | this.edge = edge; 31 | } 32 | 33 | public List[] getAdjacencyList() { 34 | return adjacencyList; 35 | } 36 | 37 | public void setAdjacencyList(List[] adjacencyList) { 38 | this.adjacencyList = adjacencyList; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/linked_list/DeleteMiddle.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.linked_list; 2 | 3 | import org.wcong.test.algorithm.basic.LinkedList; 4 | 5 | /** 6 | * give a linked list delete the middle node of it 7 | * 8 | * @author wcong 9 | * @since 17/04/2017 10 | */ 11 | public class DeleteMiddle { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | public static void deleteMiddle(LinkedList.Node root) { 18 | if (root == null) { 19 | return; 20 | } 21 | LinkedList.Node current = root; 22 | LinkedList.Node middle = root; 23 | int middleCount = 1; 24 | int count = 0; 25 | while (current != null) { 26 | count += 1; 27 | current = current.next; 28 | while (middleCount < (count / 2)) { 29 | middle = middle.next; 30 | middleCount += 1; 31 | } 32 | } 33 | middle.next = middle.next.next; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/NUmbersOfIslandsTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.NumbersOfIslands; 6 | 7 | /** 8 | * @author wcong 9 | * @since 09/05/2017 10 | */ 11 | public class NUmbersOfIslandsTest { 12 | 13 | @Test 14 | public void testNumIslandsBruteForce() { 15 | NumbersOfIslands numbersOfIslands = new NumbersOfIslands(); 16 | char[][] chars1 = new char[][] { "11110".toCharArray(), "11010".toCharArray(), "11000".toCharArray(), 17 | "00000".toCharArray() }; 18 | Assert.assertTrue(numbersOfIslands.numIslandsBruteForce(chars1) == 1); 19 | char[][] chars2 = new char[][] { "11000".toCharArray(), "11000".toCharArray(), "00100".toCharArray(), 20 | "00011".toCharArray() }; 21 | Assert.assertTrue(numbersOfIslands.numIslandsBruteForce(chars2) == 3); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/WordSearch2Test.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.WordSearch2; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author wcong 11 | * @since 26/05/2017 12 | */ 13 | public class WordSearch2Test { 14 | 15 | @Test 16 | public void testWordSearch2() { 17 | char[][] board = new char[][]{{'o', 'a', 'a', 'n'}, 18 | {'e', 't', 'a', 'e'}, 19 | {'i', 'h', 'k', 'r'}, 20 | {'i', 'f', 'l', 'v'}}; 21 | WordSearch2 wordSearch2 = new WordSearch2(); 22 | List result = wordSearch2.findWords(board, new String[]{"oath", "pea", "eat", "rain"}); 23 | Assert.assertTrue(result.get(0).equals("oath")); 24 | Assert.assertTrue(result.get(1).equals("eat")); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/RodCutting.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * given a rod of length n inches and a table of n prices p[i] for i=1,2,3.... 5 | * determine the maximum revenue r(n) by cutting up rod and selling prices 6 | * Created by wcong on 2017/3/30. 7 | */ 8 | public class RodCutting { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | public static int rodCutting(int[] array, int n) { 15 | int[] revenueArray = new int[n + 1]; 16 | for (int i = 1; i <= n; i++) { 17 | int max = array[i]; 18 | for (int j = 1; j < i; j++) { 19 | int revenue = revenueArray[j] + revenueArray[i - j]; 20 | if (revenue > max) { 21 | max = revenue; 22 | } 23 | } 24 | revenueArray[i] = max; 25 | } 26 | return revenueArray[n]; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /doc/bean/CustomizeBeanAnnotation.md: -------------------------------------------------------------------------------- 1 | #### Spring Bean Annotation 2 | 3 | * org.springframework.stereotype.Component 4 | * org.springframework.stereotype.Repository 5 | * org.springframework.stereotype.Service 6 | * org.springframework.stereotype.Controller 7 | 8 | #### Basic Spring Bean Annotation 9 | 10 | org.springframework.stereotype.Component is the root annotated Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning 11 | 12 | #### Customize Bean Annotation 13 | 14 | So,customize annotation is easy 15 | 16 | ``` java 17 | 18 | @Target({ ElementType.TYPE }) 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Documented 21 | @Component 22 | public @interface MyComponent { 23 | String value() default ""; 24 | } 25 | 26 | ``` 27 | 28 | *value()* is important for generate bean name 29 | 30 | **[Full Examples](../../src/main/java/org/wcong/test/spring/ComponentAnnotationTest.java)** 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/KthSmallestLexicographicalOrderTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.KthSmallestLexicographicalOrder; 6 | 7 | /** 8 | * @author wcong 9 | * @since 06/06/2017 10 | */ 11 | public class KthSmallestLexicographicalOrderTest { 12 | 13 | 14 | @Test 15 | public void testFindKthNumber() { 16 | KthSmallestLexicographicalOrder solution = new KthSmallestLexicographicalOrder(); 17 | Assert.assertTrue(solution.findKthNumberBruteForce(13, 2) == 10); 18 | Assert.assertTrue(solution.findKthNumberGreed(13, 2) == 10); 19 | Assert.assertTrue(solution.findKthNumberGreed(132, 4) == 101); 20 | Assert.assertTrue(solution.findKthNumberGreed(10, 3) == 2); 21 | Assert.assertTrue(solution.findKthNumberGreed(1000, 1000) == 9999); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/tree/MirrorOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.tree; 2 | 3 | import org.wcong.test.algorithm.jzoffer.util.Tree; 4 | import org.wcong.test.algorithm.jzoffer.util.TreeNode; 5 | 6 | /** 7 | * test for binary tree 8 | *

9 | * give a binary tree return a mirror of it,left to right and right to left 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class MirrorOfBinaryTree { 15 | 16 | public static void main(String[] args) { 17 | 18 | } 19 | 20 | public static void mirrorOfBinaryTree(Tree tree) { 21 | mirrorOfNode(tree.root); 22 | } 23 | 24 | private static void mirrorOfNode(TreeNode treeNode) { 25 | if (treeNode == null) { 26 | return; 27 | } 28 | TreeNode temp = treeNode.left; 29 | treeNode.left = treeNode.right; 30 | treeNode.right = temp; 31 | mirrorOfNode(treeNode.left); 32 | mirrorOfNode(treeNode.right); 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/HeapSort.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | /** 4 | * a*b,b*c,c*d,d*e 5 | * 6 | * Created by hzwangcong on 2017/2/5. 7 | */ 8 | public class HeapSort { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | static void maxHeapify(int[] array, int top) { 15 | if (top >= array.length) { 16 | return; 17 | } 18 | int left = (top + 1) * 2 - 1; 19 | int right = (top + 1) * 2; 20 | int large = top; 21 | if (left < array.length && array[left] > array[large]) { 22 | large = left; 23 | } 24 | if (right < array.length && array[right] > array[large]) { 25 | large = right; 26 | } 27 | if (large != top) { 28 | int temp = array[top]; 29 | array[top] = array[large]; 30 | array[large] = temp; 31 | maxHeapify(array, large); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/InorderSuccessorInBST.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | /** 4 | * give a binary search tree and a node in it,find the inorder successor of the node in the bst 5 | * test for inorder tree walk, 6 | * if right child is not null,find the min node in left sub tree; 7 | * if right child is null,find the last right ancestor. 8 | * Created by wcong on 2017/4/1. 9 | */ 10 | public class InorderSuccessorInBST { 11 | 12 | public static void main(String[] args) { 13 | 14 | } 15 | 16 | public static TreeNode inOrderSuccessor(TreeNode root, TreeNode findNode) { 17 | TreeNode lastLeftNode = null; 18 | TreeNode compareNode = root; 19 | while (compareNode != null) { 20 | if (findNode.val < compareNode.val) { 21 | lastLeftNode = compareNode; 22 | compareNode = compareNode.left; 23 | } else { 24 | compareNode = compareNode.right; 25 | } 26 | } 27 | return lastLeftNode; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/PhaserPrintTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.Phaser; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * @author wcong 12 | * @since 30/04/2017 13 | */ 14 | public class PhaserPrintTest { 15 | 16 | @Test 17 | public void test() { 18 | final Phaser phaser = new Phaser(10); // this will add to 10 emedialy,do not register again; 19 | ExecutorService executorService = Executors.newFixedThreadPool(10); 20 | for (int threadNum = 0; threadNum < 10; threadNum++) { 21 | executorService.submit(new PhaserPrint(phaser)); 22 | } 23 | executorService.shutdown(); 24 | try { 25 | executorService.awaitTermination(10000L, TimeUnit.MILLISECONDS); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/ComponentAnnotationTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @author wcong 8 | * @since 16/1/21 9 | */ 10 | @Configuration 11 | public class ComponentAnnotationTest { 12 | 13 | public static void main(String[] args) { 14 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 15 | annotationConfigApplicationContext.register(ComponentAnnotationTest.class); 16 | annotationConfigApplicationContext.refresh(); 17 | InjectClass injectClass = annotationConfigApplicationContext.getBean(InjectClass.class); 18 | injectClass.print(); 19 | } 20 | 21 | @MyComponent 22 | public static class InjectClass { 23 | 24 | public void print() { 25 | System.out.println("hello world"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/CountDownLatchThreadTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * @author wcong 12 | * @since 30/04/2017 13 | */ 14 | public class CountDownLatchThreadTest { 15 | 16 | @Test 17 | public void test() { 18 | CountDownLatch countDownLatch = new CountDownLatch(10); 19 | ExecutorService executorService = Executors.newFixedThreadPool(10); 20 | for (int threadNum = 0; threadNum < 10; threadNum++) { 21 | executorService.submit(new CountDownLatchThread(countDownLatch)); 22 | } 23 | executorService.shutdown(); 24 | try { 25 | executorService.awaitTermination(100000L, TimeUnit.MILLISECONDS); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/MoreThanHalfInArray.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | 5 | /** 6 | * test for array 7 | * give a array and there is a num appear more than a half find it 8 | * Created by hzwangcong on 2017/3/27. 9 | */ 10 | public class MoreThanHalfInArray { 11 | 12 | public static void main(String[] args) { 13 | Assert.isTrue(halfNum(new int[]{1, 2, 1, 1, 3, 1, 4}) == 1); 14 | } 15 | 16 | public static int halfNum(int[] array) { 17 | int num = array[0]; 18 | int count = 1; 19 | for (int i = 1; i < array.length; i++) { 20 | if (array[i] == num) { 21 | count += 1; 22 | } else { 23 | count -= 1; 24 | } 25 | if (count <= 0) { 26 | num = array[i]; 27 | count = 1; 28 | } 29 | } 30 | return num; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/ReversePiarsTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.ReversePairs; 6 | 7 | /** 8 | * @author wcong 9 | * @since 04/05/2017 10 | */ 11 | public class ReversePiarsTest { 12 | 13 | @Test 14 | public void test() { 15 | int[] nums = new int[] { 1, 3, 4, 6, 8, 2 }; 16 | int[] maxInt = new int[] { 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 }; 17 | ReversePairs reversePairs = new ReversePairs(); 18 | Assert.assertTrue(reversePairs.reversePairsBruteForce(nums) == 2); 19 | Assert.assertTrue(reversePairs.reversePairsDivideAndConquer(nums) == 2); 20 | Assert.assertTrue(reversePairs.reversePairsDivideAndConquer(maxInt) == 0); 21 | Assert.assertTrue(reversePairs.reversePairsLiner(new int[] { 1, 3, 4, 6, 8, 2 }) == 2); 22 | Assert.assertTrue(reversePairs.reversePairsLiner(maxInt) == 0); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/array_and_string/IsUnique.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.array_and_string; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * implement an algorithm to determine if a string has all unique characters 8 | * what if you cannot use additional data structures 9 | *

10 | * 1. none additional space and cannot change original string O(n2) 11 | * 2. none additional space and can change original string O(n log n) 12 | * 3. additional space hashMap and O(n) 13 | * 14 | * @author wcong 15 | * @since 17/04/2017 16 | */ 17 | public class IsUnique { 18 | 19 | public static void main(String[] args) { 20 | 21 | } 22 | 23 | public boolean isUnique(String string) { 24 | Map count = new HashMap<>(); 25 | for (char inner : string.toCharArray()) { 26 | if (count.containsKey(inner)) { 27 | return false; 28 | } 29 | count.put(inner, 1); 30 | } 31 | return true; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/util/BuildTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.util; 2 | 3 | /** 4 | * @author wcong 5 | * @since 2017/3/26 6 | */ 7 | public class BuildTree { 8 | 9 | public static Tree buildOne(int[] array) { 10 | Tree tree = new Tree(); 11 | TreeNode treeNode = new TreeNode(); 12 | treeNode.value = array[0]; 13 | tree.root = treeNode; 14 | buildTree(treeNode, array, 0); 15 | return tree; 16 | } 17 | 18 | private static void buildTree(TreeNode treeNode, int[] array, int h) { 19 | int leftNode = h * 2 + 1; 20 | if (leftNode < array.length) { 21 | TreeNode left = new TreeNode(); 22 | left.value = array[leftNode]; 23 | treeNode.left = left; 24 | buildTree(left, array, leftNode); 25 | } 26 | int rightNode = h * 2 + 2; 27 | if (rightNode < array.length) { 28 | TreeNode right = new TreeNode(); 29 | right.value = array[rightNode]; 30 | treeNode.right = right; 31 | buildTree(right, array, rightNode); 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/ProducerAndComsumer.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * @author wcong 7 | * @since 29/04/2017 8 | */ 9 | public class ProducerAndComsumer { 10 | 11 | private static class Queue { 12 | 13 | private int maxSize; 14 | 15 | public Queue(int maxSize) { 16 | this.maxSize = maxSize; 17 | } 18 | 19 | LinkedList integerList = new LinkedList<>(); 20 | 21 | public synchronized Integer get() { 22 | while (integerList.isEmpty()) { 23 | try { 24 | wait(); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | return integerList.pop(); 30 | 31 | } 32 | 33 | public synchronized void add(Integer num) { 34 | while (maxSize == integerList.size()) { 35 | try { 36 | wait(); 37 | } catch (InterruptedException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | integerList.push(num); 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/MaxSumSubArray.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for array 7 | * give a array find the max sum of sub array 8 | * for example 1,-2,3,10,-4,7,2,-5 max 18 9 | * Created by hzwangcong on 2017/3/28. 10 | */ 11 | public class MaxSumSubArray { 12 | 13 | 14 | public static void main(String[] args) { 15 | Assert.isTrue(maxSum(new int[]{1, -2, 3, 10, -4, 7, 2, -5}) == 18); 16 | } 17 | 18 | public static int maxSum(int[] array) { 19 | int maxSum = array[0]; 20 | int currentSum = array[0]; 21 | for (int i = 1; i < array.length; i++) { 22 | if (currentSum <= 0) { 23 | currentSum = array[i]; 24 | }else{ 25 | currentSum+=array[i]; 26 | } 27 | if (currentSum > maxSum) { 28 | maxSum = currentSum; 29 | } 30 | } 31 | return maxSum; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/PhaserPrint.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.Phaser; 4 | 5 | /** 6 | * @author wcong 7 | * @since 30/04/2017 8 | */ 9 | public class PhaserPrint implements Runnable { 10 | 11 | private Phaser phaser; 12 | 13 | public PhaserPrint(Phaser phaser) { 14 | this.phaser = phaser; 15 | } 16 | 17 | @Override 18 | public void run() { 19 | System.out.println("thread:" + Thread.currentThread().getId() + ":print 1"); 20 | phaser.arriveAndAwaitAdvance(); 21 | System.out.println("thread:" + Thread.currentThread().getId() + ":print 2"); 22 | phaser.arriveAndAwaitAdvance(); 23 | System.out.println("thread:" + Thread.currentThread().getId() + ":print 3"); 24 | phaser.arriveAndAwaitAdvance(); 25 | System.out.println("thread:" + Thread.currentThread().getId() + ":print 4"); 26 | phaser.arriveAndAwaitAdvance(); 27 | System.out.println("thread:" + Thread.currentThread().getId() + ":finished"); 28 | phaser.arriveAndDeregister(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/SumOfLeftLeaves.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * sum all of the leaves of the tree 7 | * Created by wcong on 2017/4/1. 8 | */ 9 | public class SumOfLeftLeaves { 10 | 11 | public static void main(String[] args) { 12 | 13 | } 14 | 15 | public static int sumLeft(TreeNode root) { 16 | int sum = 0; 17 | if (root == null) { 18 | return sum; 19 | } 20 | Stack treeNodeStack = new Stack<>(); 21 | treeNodeStack.push(root); 22 | while (!treeNodeStack.isEmpty()) { 23 | TreeNode treeNode = treeNodeStack.pop(); 24 | if (treeNode.right != null) { 25 | treeNodeStack.push(treeNode.right); 26 | } 27 | if (treeNode.left != null) { 28 | sum += treeNode.left.val; 29 | treeNodeStack.push(treeNode.left); 30 | } 31 | } 32 | return sum; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/BeanFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.beans.factory.config.BeanDefinition; 4 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; 5 | import org.springframework.beans.factory.support.GenericBeanDefinition; 6 | 7 | /** 8 | * @author wcong 9 | * @since 16/2/3 10 | */ 11 | public class BeanFactoryTest { 12 | 13 | public static void main(String[] args) { 14 | DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); 15 | BeanDefinition beanDefinition = new GenericBeanDefinition(); 16 | beanDefinition.setBeanClassName(BeanTest.class.getName()); 17 | beanFactory.registerBeanDefinition(beanDefinition.getBeanClassName(), beanDefinition); 18 | BeanTest beanTest = beanFactory.getBean(beanDefinition.getBeanClassName(), BeanTest.class); 19 | beanTest.hello(); 20 | } 21 | 22 | public static class BeanTest { 23 | 24 | public void hello() { 25 | System.out.println("hello"); 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/guice/SetBinderTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.guice; 2 | 3 | import com.google.inject.AbstractModule; 4 | import com.google.inject.Guice; 5 | import com.google.inject.Injector; 6 | import com.google.inject.Key; 7 | import com.google.inject.TypeLiteral; 8 | import com.google.inject.multibindings.Multibinder; 9 | 10 | import java.util.List; 11 | import java.util.Set; 12 | 13 | /** 14 | * @author wcong 15 | * @since 2016/10/31 16 | */ 17 | public class SetBinderTest { 18 | 19 | public static void main(String[] args) { 20 | Injector injector = Guice.createInjector(new AbstractModule() { 21 | @Override 22 | protected void configure() { 23 | Multibinder multibinder = Multibinder.newSetBinder(binder(), String.class); 24 | multibinder.addBinding().toInstance("hello"); 25 | multibinder.addBinding().toInstance("world"); 26 | } 27 | }); 28 | Set stringList = injector.getInstance(Key.get(new TypeLiteral>() { 29 | })); 30 | System.out.println(stringList); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/EditDistance.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give two strings a,b calculate the distance for b transform to b 5 | * use 6 | * add insert one char 7 | * delete delete one char 8 | * replace replace one char 9 | * return the minimum distance 10 | * Created by wcong on 2017/3/31. 11 | */ 12 | public class EditDistance { 13 | 14 | public static void main() { 15 | } 16 | 17 | public static int mininumEditDistance(String a, String b) { 18 | int[][] result = new int[a.length()][b.length()]; 19 | for (int i = 0; i < a.length(); i++) { 20 | for (int j = 0; j < b.length(); j++) { 21 | if (a.charAt(i) == b.charAt(j)) { 22 | result[i + 1][j + 1] = result[i][j] + 1; 23 | } else { 24 | result[i + 1][j + 1] = 1 + Math.min(Math.min(result[i + 1][j], result[i][j + 1]), result[i][j]); 25 | } 26 | } 27 | } 28 | return result[a.length()][b.length()]; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/tree/PrintBinaryTreeTopDown.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.tree; 2 | 3 | import org.wcong.test.algorithm.jzoffer.util.Tree; 4 | import org.wcong.test.algorithm.jzoffer.util.TreeNode; 5 | 6 | import java.util.LinkedList; 7 | import java.util.Queue; 8 | 9 | /** 10 | * test for binary tree 11 | * 12 | * @author wcong 13 | * @since 2017/3/26 14 | */ 15 | public class PrintBinaryTreeTopDown { 16 | 17 | public static void main(String[] args) { 18 | 19 | } 20 | 21 | public static void printTreeTopDown(Tree tree) { 22 | if (tree == null || tree.root == null) { 23 | return; 24 | } 25 | Queue treeNodeQueue = new LinkedList<>(); 26 | treeNodeQueue.add(tree.root); 27 | while (!treeNodeQueue.isEmpty()) { 28 | TreeNode node = treeNodeQueue.poll(); 29 | System.out.println(node.value); 30 | if (node.left != null) { 31 | treeNodeQueue.add(node.left); 32 | } 33 | if (node.right != null) { 34 | treeNodeQueue.add(node.right); 35 | } 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/BinaryTreePaths.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * given a binary tree,return all root-to-leaf path 8 | * Created by wcong on 2017/4/1. 9 | */ 10 | public class BinaryTreePaths { 11 | 12 | static class TreeNode { 13 | int value; 14 | TreeNode left; 15 | TreeNode right; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | } 21 | 22 | public List paths(TreeNode treeNode) { 23 | List path = new ArrayList<>(); 24 | findPath(treeNode, "", path); 25 | return path; 26 | } 27 | 28 | private static void findPath(TreeNode treeNode, String prefix, List path) { 29 | if (treeNode == null) { 30 | path.add(prefix); 31 | return; 32 | } 33 | prefix += treeNode.value; 34 | findPath(treeNode.left, prefix, path); 35 | findPath(treeNode.right, prefix, path); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/CyclicBarrierPrint.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.BrokenBarrierException; 4 | import java.util.concurrent.CyclicBarrier; 5 | 6 | /** 7 | * @author wcong 8 | * @since 30/04/2017 9 | */ 10 | public class CyclicBarrierPrint implements Runnable { 11 | 12 | private CyclicBarrier cyclicBarrier; 13 | 14 | public CyclicBarrierPrint(CyclicBarrier cyclicBarrier) { 15 | this.cyclicBarrier = cyclicBarrier; 16 | } 17 | 18 | @Override 19 | public void run() { 20 | System.out.println("I'm ready"); 21 | try { 22 | cyclicBarrier.await(); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } catch (BrokenBarrierException e) { 26 | e.printStackTrace(); 27 | } 28 | System.out.println("I'm ready 1"); 29 | try { 30 | cyclicBarrier.await(); 31 | } catch (InterruptedException e) { 32 | e.printStackTrace(); 33 | } catch (BrokenBarrierException e) { 34 | e.printStackTrace(); 35 | } 36 | System.out.println("we are finished"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/MaximumPathSum.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | /** 4 | * give a binary tree,find the maximum path sum for any path ,not have to include the root,just path 5 | * Created by wcong on 2017/4/1. 6 | */ 7 | public class MaximumPathSum { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | static class TreeNode { 14 | int value; 15 | TreeNode left; 16 | TreeNode right; 17 | } 18 | 19 | static int max = Integer.MIN_VALUE; 20 | 21 | public static int maximumPathSum(TreeNode root) { 22 | sumTree(root); 23 | return max; 24 | } 25 | 26 | private static int sumTree(TreeNode treeNode) { 27 | if (treeNode == null) { 28 | return 0; 29 | } 30 | int sum = treeNode.value; 31 | sum += Math.max(sumTree(treeNode.left), 0); 32 | sum += Math.max(sumTree(treeNode.right), 0); 33 | if (sum > max) { 34 | max = sum; 35 | } 36 | return sum; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/StrongPassword.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | /** 4 | * A password is considered strong if below conditions are all met: 5 | * 1. It has at least 6 characters and at most 20 characters. 6 | * 2. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. 7 | * 3. It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met). 8 | * Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0. 9 | * Insertion, deletion or replace of any one character are all considered as one change. 10 | * 11 | * @author wcong 12 | * @since 07/05/2017 13 | */ 14 | public class StrongPassword { 15 | 16 | public int strongPasswordDP(String s) { 17 | int[][] distance = new int[s.length()][s.length()]; 18 | 19 | return distance[s.length()][s.length()]; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/linked_list/LoopDetection.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.linked_list; 2 | 3 | import org.wcong.test.algorithm.basic.LinkedList; 4 | 5 | /** 6 | * given a circle linked list 7 | * implement an algorithm that return the node at the beginning of the loop 8 | * 9 | * @author wcong 10 | * @since 17/04/2017 11 | */ 12 | public class LoopDetection { 13 | 14 | public static void main(String[] args) { 15 | 16 | } 17 | 18 | public static LinkedList.Node begining(LinkedList.Node list) { 19 | LinkedList.Node step1 = list; 20 | LinkedList.Node step2 = list; 21 | while (true) { 22 | if (step1 == null || step2 == null) { 23 | return null; 24 | } 25 | step1 = step1.next; 26 | if (step2.next == null) { 27 | return null; 28 | } 29 | step2 = step2.next.next; 30 | if (step1 == step2) { 31 | break; 32 | } 33 | } 34 | step1 = list; 35 | while (true) { 36 | if (step1 == step2) { 37 | return step1; 38 | } 39 | step1 = step1.next; 40 | step2 = step2.next; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/PrintNumberOfN.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer; 2 | 3 | /** 4 | * test number limit boundary 5 | * give a number n print all the numbers of decimal 6 | * 7 | * @author wcong 8 | * @since 2017/3/26 9 | */ 10 | public class PrintNumberOfN { 11 | 12 | public static void main(String[] args) { 13 | printNumberOfN(3); 14 | } 15 | 16 | public static void printNumberOfN(int n) { 17 | char[] number = new char[n]; 18 | printInteger(number, 0, n - 1); 19 | } 20 | 21 | private static void printInteger(char[] number, int index, int n) { 22 | if (index > n) { 23 | int startIndex = 0; 24 | while (startIndex < n) { 25 | if (number[startIndex] == 48) { 26 | startIndex += 1; 27 | } else { 28 | break; 29 | } 30 | } 31 | while (startIndex <= n) { 32 | System.out.print(number[startIndex++]); 33 | } 34 | System.out.print("\n"); 35 | return; 36 | } 37 | for (int j = 48; j < 58; j++) { 38 | number[index] = (char) j; 39 | printInteger(number, index + 1, n); 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/DecodeWays.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | 5 | /** 6 | * string also a dynamic programming 7 | * encode way,A->1,....,Z->26 8 | * give a string of numbers,count all the ways can be decoded 9 | * for example 12 can be decode two ways 1->A,2->B or 12->L 10 | * Created by wcong on 2017/4/1. 11 | */ 12 | public class DecodeWays { 13 | 14 | public static void main(String[] args) { 15 | Assert.isTrue(decodeWays("123") == 3); 16 | } 17 | 18 | public static int decodeWays(String encode) { 19 | int[] result = new int[encode.length() + 1]; 20 | result[0] = 1; 21 | result[1] = 1; 22 | for (int i = 1; i < encode.length(); i++) { 23 | if (Integer.parseInt(encode.substring(i - 1, i + 1)) <= 26) { 24 | result[i + 1] = result[i - 1] + result[i]; 25 | } else { 26 | result[i + 1] = result[i]; 27 | } 28 | } 29 | return result[encode.length()]; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/basic/Graph.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.basic; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * represent a graph 8 | * adjacent list 9 | * Created by wcong on 2017/3/29. 10 | */ 11 | public class Graph { 12 | 13 | 14 | public static class Edge { 15 | 16 | public Vertex from; 17 | 18 | public Vertex to; 19 | 20 | public int distance; 21 | 22 | } 23 | 24 | public static class Vertex { 25 | public int value; 26 | 27 | public int distance; 28 | 29 | public Vertex last; 30 | 31 | @Override 32 | public boolean equals(Object o) { 33 | if (o == null || !(o instanceof Vertex)) { 34 | return false; 35 | } 36 | return value == ((Vertex) o).value; 37 | } 38 | } 39 | 40 | public List edges; 41 | 42 | public Map> adjacentMap; 43 | 44 | public void addEdge(Edge edge) { 45 | edges.add(edge); 46 | adjacentMap.get(edge.from).add(edge.to); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/guice/TwoModuleTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.guice; 2 | 3 | import com.google.inject.AbstractModule; 4 | import com.google.inject.Guice; 5 | import com.google.inject.Injector; 6 | import com.google.inject.Singleton; 7 | 8 | /** 9 | * Created by wcong on 2016/9/19. 10 | */ 11 | public class TwoModuleTest { 12 | 13 | public interface Print { 14 | void print(); 15 | } 16 | 17 | @Singleton 18 | public static class HelloPrint implements Print { 19 | 20 | public void print() { 21 | System.out.println("hello"); 22 | } 23 | } 24 | 25 | 26 | public static void main(String[] args) { 27 | Injector injector = Guice.createInjector(new AbstractModule() { 28 | @Override 29 | protected void configure() { 30 | bind(Print.class).to(HelloPrint.class); 31 | } 32 | }); 33 | Print print = injector.getInstance(Print.class); 34 | System.out.println(print); 35 | print = injector.getInstance(Print.class); 36 | System.out.println(print); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/OnMyPropertiesCondition.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 4 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 5 | import org.springframework.context.annotation.ConditionContext; 6 | import org.springframework.core.type.AnnotatedTypeMetadata; 7 | 8 | /** 9 | * Created by wcong on 2016/6/13. 10 | */ 11 | public class OnMyPropertiesCondition extends SpringBootCondition { 12 | 13 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 14 | Object propertiesName = metadata.getAnnotationAttributes(ConditionalOnMyProperties.class.getName()).get("name"); 15 | if (propertiesName != null) { 16 | String value = context.getEnvironment().getProperty(propertiesName.toString()); 17 | if (value != null) { 18 | return new ConditionOutcome(true, "get properties"); 19 | } 20 | } 21 | return new ConditionOutcome(false, "none get properties"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/linked_list/DeleteNodeInO1.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.linked_list; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for linked list basic operate,search,add,delete 7 | * give a linked list root and delete node,delete this node in O(1) time 8 | * 9 | * @author wcong 10 | * @since 2017/3/26 11 | */ 12 | public class DeleteNodeInO1 { 13 | 14 | public static void main(String[] args) { 15 | } 16 | 17 | public static class LinkedNode { 18 | int value; 19 | 20 | LinkedNode next; 21 | } 22 | 23 | public static LinkedNode deleteInO1(LinkedNode root, LinkedNode delete) { 24 | if (root == null || delete == null) { 25 | return null; 26 | } 27 | if (delete.next != null) { 28 | delete.value = delete.next.value; 29 | delete.next = delete.next.next; 30 | } else if (root == delete) { 31 | return null; 32 | } else { 33 | LinkedNode parent = root; 34 | while (parent.next != delete) { 35 | parent = parent.next; 36 | } 37 | parent.next = null; 38 | } 39 | return root; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/TextJustifacation.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Given an array of words and a length L, 7 | * format the text such that each line has exactly L characters and is fully (left and right) justified. 8 | *

9 | * You should pack your words in a greedy approach; 10 | * that is, pack as many words as you can in each line. 11 | * Pad extra spaces ' ' when necessary so that each line has exactly L characters. 12 | *

13 | * Extra spaces between words should be distributed as evenly as possible. 14 | * If the number of spaces on a line do not divide evenly between words, 15 | * the empty slots on the left will be assigned more spaces than the slots on the right. 16 | *

17 | * For the last line of text, it should be left justified and no extra space is inserted between words. 18 | * 19 | * @author wcong 20 | * @since 05/05/2017 21 | */ 22 | public class TextJustifacation { 23 | public List fullJustify(String[] words, int maxWidth) { 24 | return null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/aop/MethodInterceptorImpl.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.aop; 2 | 3 | import org.springframework.cglib.proxy.MethodInterceptor; 4 | import org.springframework.cglib.proxy.MethodProxy; 5 | 6 | import java.lang.reflect.Method; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * @author wcong 12 | * @since 16/4/18 13 | */ 14 | public class MethodInterceptorImpl implements MethodInterceptor { 15 | private Map> advisorMap; 16 | 17 | public MethodInterceptorImpl(Map> advisorMap) { 18 | this.advisorMap = advisorMap; 19 | } 20 | 21 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { 22 | List advisorList = advisorMap.get(method); 23 | if (advisorList == null) { 24 | return methodProxy.invokeSuper(o, objects); 25 | } else { 26 | MethodInvocation methodInvocation = new MethodInvocation(o, method, objects, methodProxy, advisorList); 27 | return methodInvocation.proceed(methodInvocation); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/MyEnableAutoConfigurationImport.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.beans.factory.BeanClassLoaderAware; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.annotation.DeferredImportSelector; 6 | import org.springframework.core.io.support.SpringFactoriesLoader; 7 | import org.springframework.core.type.AnnotationMetadata; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * MyEnableAutoConfigurationImport 13 | * Created by wcong on 2016/6/3. 14 | */ 15 | public class MyEnableAutoConfigurationImport implements DeferredImportSelector, BeanClassLoaderAware { 16 | private ClassLoader classLoader; 17 | 18 | public void setBeanClassLoader(ClassLoader classLoader) { 19 | this.classLoader = classLoader; 20 | } 21 | 22 | public String[] selectImports(AnnotationMetadata importingClassMetadata) { 23 | List beanNames = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, classLoader); 24 | return beanNames.toArray(new String[beanNames.size()]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/dp_divide_conque/MagicIndex.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.dp_divide_conque; 2 | 3 | /** 4 | * a magic index in an array A[1....n-1] is defined to be index such that A[i]=i 5 | * given a sorted array of distinct integers 6 | * write a method to find the magic index,if one exist in array an index 7 | * 8 | * @author wcong 9 | * @since 19/04/2017 10 | */ 11 | public class MagicIndex { 12 | 13 | public int magicIndexDistinct(int[] array) { 14 | int start = 0, end = array.length - 1; 15 | while (start < end) { 16 | int middle = start + (end - start) / 2; 17 | if (array[middle] == middle) { 18 | return middle; 19 | } else if (array[middle] > middle) { 20 | end = middle - 1; 21 | } else { 22 | start = middle + 1; 23 | } 24 | } 25 | return -1; 26 | } 27 | 28 | public int magicIndexNormal(int[] array) { 29 | int i = 0; 30 | while (i < array.length) { 31 | if (array[i] == i) { 32 | return i; 33 | } else if (array[i] > i) { 34 | i += 1; 35 | } else { 36 | i = array[i] + 1; 37 | } 38 | } 39 | return -1; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/CustomizeEnableAutoConfigure.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.ResponseBody; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * Created by wcong on 2016/6/1. 14 | */ 15 | @Configuration 16 | @MyEnableAutoConfiguration 17 | public class CustomizeEnableAutoConfigure { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication application = new SpringApplication(CustomizeEnableAutoConfigure.class); 21 | application.run(args); 22 | } 23 | 24 | @Controller 25 | public static class MyController { 26 | @RequestMapping 27 | @ResponseBody 28 | public Map index() { 29 | Map map = new HashMap(); 30 | map.put("hello", "world"); 31 | return map; 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/BestTimeBuySellStockIVTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.BestTimeBuySellStockIV; 6 | 7 | /** 8 | * @author wcong 9 | * @since 10/06/2017 10 | */ 11 | public class BestTimeBuySellStockIVTest { 12 | 13 | @Test 14 | public void testMaxProfit() { 15 | BestTimeBuySellStockIV solution = new BestTimeBuySellStockIV(); 16 | int[] simple = new int[]{0, 1}; 17 | int[] oneMax = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}; 18 | int[] transaction = new int[]{1, 2}; 19 | int[] time = new int[]{48, 12, 60, 93, 97, 42, 25, 64, 17, 56, 85, 93, 9, 48, 52, 42, 58, 85, 81, 84, 69, 36, 1, 54, 23, 15, 72, 15, 11, 94}; 20 | int[] border = new int[]{1, 4, 2}; 21 | Assert.assertTrue(solution.maxProfitDp(2, simple) == 1); 22 | Assert.assertTrue(solution.maxProfitDp(9, oneMax) == 8); 23 | Assert.assertTrue(solution.maxProfitDp(1, transaction) == 1); 24 | Assert.assertTrue(solution.maxProfitDp(2, border) == 3); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/BreakingAString.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * given a string S with n characters and an array containing the break points, compute 5 | * the lowest cost for a sequence of breaks, along with a sequence of breaks that 6 | * achieves this cost. 7 | * length of 20 points 2,8,10 8 | * Created by wcong on 2017/3/31. 9 | */ 10 | public class BreakingAString { 11 | public static void main(String[] args) { 12 | 13 | } 14 | 15 | public static int minimumCost(String a, int[] point) { 16 | return minimumCost(a, 0, a.length() - 1, point, 0, point.length - 1); 17 | } 18 | 19 | public static int minimumCost(String a, int start, int end, int[] point, int pointStart, int pointEnd) { 20 | if (start >= end || pointStart >= pointEnd) { 21 | return 0; 22 | } 23 | int length = end - start + 1; 24 | int left = minimumCost(a, point[pointStart], end, point, pointStart + 1, pointEnd); 25 | int right = minimumCost(a, start, point[pointEnd], point, pointStart, pointEnd - 1); 26 | return left < right ? left + length : right + length; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/Bootstrap.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * Created by wcong on 2016/5/26. 15 | */ 16 | @Configuration 17 | @EnableAutoConfiguration 18 | public class Bootstrap { 19 | 20 | public static void main(String[] args) { 21 | SpringApplication springApplication = new SpringApplication(Bootstrap.class); 22 | springApplication.run(args); 23 | } 24 | 25 | @Controller 26 | public static class MyController { 27 | @RequestMapping 28 | @ResponseBody 29 | public Map index() { 30 | Map map = new HashMap(); 31 | map.put("hello", "world"); 32 | return map; 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/WordBreak2Test.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.WordBreak2; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | /** 11 | * @author wcong 12 | * @since 24/05/2017 13 | */ 14 | public class WordBreak2Test { 15 | 16 | @Test 17 | public void testWordBreak2() { 18 | List dict = Arrays.asList("cat", "cats", "and", "sand", "dog"); 19 | WordBreak2 wordBreak2 = new WordBreak2(); 20 | List result = wordBreak2.wordBreakBruteForce("catsanddog", dict); 21 | Assert.assertTrue(result.get(0).equals("cat sand dog")); 22 | Assert.assertTrue(result.get(1).equals("cats and dog")); 23 | List resultDp = wordBreak2.wordBreakDp("catsanddog", dict); 24 | Assert.assertTrue(resultDp.get(1).equals("cat sand dog")); 25 | Assert.assertTrue(resultDp.get(0).equals("cats and dog")); 26 | List dictNone = Arrays.asList("aaaa", "aa"); 27 | List resultDpNone = wordBreak2.wordBreakDp("aaaaaaa", dictNone); 28 | Assert.assertTrue(resultDpNone.isEmpty()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/aop/AbstractAdvisor.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.aop; 2 | 3 | import java.lang.reflect.Method; 4 | import java.util.regex.Pattern; 5 | 6 | /** 7 | * @author wcong 8 | * @since 16/4/18 9 | */ 10 | public abstract class AbstractAdvisor implements Proceed { 11 | 12 | public static final int AFTER_ORDER = 0; 13 | 14 | public static final int AROUND_ORDER = 1; 15 | 16 | public static final int BEFORE_ORDER = 2; 17 | 18 | private Pattern pointCutPattern; 19 | 20 | protected String pointCut; 21 | 22 | protected Object aspectObject; 23 | 24 | protected Method aspectMethod; 25 | 26 | protected int order; 27 | 28 | public AbstractAdvisor(Object aspectObject, Method aspectMethod) { 29 | pointCut = aspectObject.getClass().getAnnotation(MyAspect.class).pointCut(); 30 | pointCutPattern = Pattern.compile(pointCut); 31 | this.aspectObject = aspectObject; 32 | this.aspectMethod = aspectMethod; 33 | } 34 | 35 | public boolean isMatch(Class matchClass, Method method) { 36 | return pointCutPattern.matcher(matchClass.getName() + "." + method.getName()).find(); 37 | } 38 | 39 | public int getOrder() { 40 | return order; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/SemaphorePrintTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | /** 10 | * @author wcong 11 | * @since 30/04/2017 12 | */ 13 | public class SemaphorePrintTest { 14 | 15 | static class PrintRunner implements Runnable { 16 | 17 | private SemaphorePrint semaphorePrint; 18 | 19 | public PrintRunner(SemaphorePrint semaphorePrint) { 20 | this.semaphorePrint = semaphorePrint; 21 | } 22 | 23 | @Override 24 | public void run() { 25 | semaphorePrint.print(); 26 | } 27 | } 28 | 29 | @Test 30 | public void test() { 31 | SemaphorePrint semaphorePrint = new SemaphorePrint(); 32 | ExecutorService executor = Executors.newFixedThreadPool(10); 33 | for (int threadNum = 0; threadNum < 10; threadNum++) { 34 | executor.submit(new PrintRunner(semaphorePrint)); 35 | } 36 | executor.shutdown(); 37 | try { 38 | executor.awaitTermination(10000L, TimeUnit.MILLISECONDS); 39 | } catch (InterruptedException e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/binary/Count1InInt.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.binary; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for binary operate and or xor and smart move to recursive 7 | * >> remain the sign bit,>>> ignore the sign bit 8 | * 9 | * @author wcong 10 | * @since 2017/3/26 11 | */ 12 | public class Count1InInt { 13 | 14 | public static void main(String[] args) { 15 | Assert.isTrue(count1Slow(0xFFFF) == count1Smart(0xFFFF)); 16 | Assert.isTrue(count1Slow(0x4444) == count1Smart(0x4444)); 17 | Assert.isTrue(count1Slow(0x3214) == count1Smart(0x3214)); 18 | Assert.isTrue(count1Slow(0x10) == count1Smart(0x10)); 19 | Assert.isTrue(count1Slow(0x1111) == count1Smart(0x1111)); 20 | } 21 | 22 | public static int count1Slow(int num) { 23 | int count = 0; 24 | while (num != 0) { 25 | if ((num & 1) != 0) { 26 | count += 1; 27 | } 28 | num = num >> 1; 29 | } 30 | return count; 31 | } 32 | 33 | public static int count1Smart(int num) { 34 | num = num < 0 ? -num : num; 35 | int count = 0; 36 | while (num > 0) { 37 | count += 1; 38 | num = num & (num - 1); 39 | } 40 | return count; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/CustomizePropertySourceLoader.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.context.ConfigurableApplicationContext; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | /** 10 | * Created by wcong on 2016/6/14. 11 | */ 12 | @Configuration 13 | @EnableAutoConfiguration 14 | public class CustomizePropertySourceLoader { 15 | 16 | @Value("${customize.property.message}") 17 | private String message; 18 | 19 | public static void main(String[] args) { 20 | SpringApplication springApplication = new SpringApplication(CustomizePropertySourceLoader.class); 21 | springApplication.setWebEnvironment(false); 22 | ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args); 23 | CustomizePropertySourceLoader customizePropertySourceLoader = configurableApplicationContext.getBean(CustomizePropertySourceLoader.class); 24 | System.out.println(customizePropertySourceLoader.message); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/concurrent/CyclicBarrierPrintTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.CyclicBarrier; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * @author wcong 12 | * @since 30/04/2017 13 | */ 14 | public class CyclicBarrierPrintTest { 15 | 16 | public static class CyclicBarrierMonitor implements Runnable { 17 | 18 | private int stage; 19 | 20 | @Override 21 | public void run() { 22 | System.out.println("we all are ready for stage :" + stage++); 23 | } 24 | } 25 | 26 | @Test 27 | public void test() { 28 | CyclicBarrier cyclicBarrier = new CyclicBarrier(10, new CyclicBarrierMonitor()); 29 | ExecutorService executorService = Executors.newFixedThreadPool(10); 30 | for (int threadNum = 0; threadNum < 10; threadNum++) { 31 | executorService.submit(new CyclicBarrierPrint(cyclicBarrier)); 32 | } 33 | executorService.shutdown(); 34 | try { 35 | executorService.awaitTermination(10000L, TimeUnit.MILLISECONDS); 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/BinaryTreeMaximumSubPath.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | /** 4 | * Given a binary tree, find the maximum path sum. 5 | * For this problem, 6 | * a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. 7 | * The path must contain at least one node and does not need to go through the root. 8 | * 9 | * @author wcong 10 | * @since 05/05/2017 11 | */ 12 | public class BinaryTreeMaximumSubPath { 13 | 14 | private int maxPath; 15 | 16 | public int maxPathSum(TreeNode root) { 17 | maxPath = Integer.MIN_VALUE; 18 | maxSubPathSum(root); 19 | return maxPath; 20 | } 21 | 22 | private int maxSubPathSum(TreeNode node) { 23 | if (node == null) { 24 | return 0; 25 | } 26 | int left = maxSubPathSum(node.left); 27 | int right = maxSubPathSum(node.right); 28 | int maxSum = node.val; 29 | if (left > 0) { 30 | maxSum += left; 31 | } 32 | if (right > 0) { 33 | maxSum += right; 34 | } 35 | if (maxPath < maxSum) { 36 | maxPath = maxSum; 37 | } 38 | int max = left > right ? left : right; 39 | return max > 0 ? max + node.val : node.val; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/mydagger/MyComponentProcessor.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.mydagger; 2 | 3 | import com.google.auto.common.BasicAnnotationProcessor; 4 | import com.squareup.javapoet.ParameterizedTypeName; 5 | 6 | import javax.annotation.processing.Filer; 7 | import javax.annotation.processing.Messager; 8 | import java.util.ArrayList; 9 | import java.util.HashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | * my component processor 15 | * 16 | * @author wcong 17 | * @since 2016/12/4 18 | */ 19 | public class MyComponentProcessor extends BasicAnnotationProcessor { 20 | 21 | private Map> dependencyMap = new HashMap<>(); 22 | 23 | @Override 24 | protected Iterable initSteps() { 25 | Filer filer = processingEnv.getFiler(); 26 | Messager messager = processingEnv.getMessager(); 27 | List processingStepList = new ArrayList<>(); 28 | processingStepList.add(new MyProvidesStep(filer, messager, dependencyMap)); 29 | processingStepList.add(new MyComponentStep(filer, messager, dependencyMap)); 30 | return processingStepList; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/dp/UglyNumber.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.dp; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for number 7 | * ugly number only contains 2,3,4 for multiply count target number of ugly number 8 | * Created by wcong on 2017/3/29. 9 | */ 10 | public class UglyNumber { 11 | 12 | public static void main(String[] args) { 13 | Assert.isTrue(nThUglyNumber(9) == 12); 14 | Assert.isTrue(nThUglyNumber(10) == 15); 15 | } 16 | 17 | public static int nThUglyNumber(int n) { 18 | int[] ugly = new int[n + 1]; 19 | ugly[0] = 1; 20 | int index2 = 0; 21 | int index3 = 0; 22 | int index5 = 0; 23 | for (int i = 1; i <= n; i++) { 24 | int min = Math.min(Math.min(ugly[index2] * 2, ugly[index3] * 3), ugly[index5] * 5); 25 | ugly[i] = min; 26 | while (ugly[index2] * 2 <= min) { 27 | index2 += 1; 28 | } 29 | while (ugly[index3] * 3 <= min) { 30 | index3 += 1; 31 | } 32 | while (ugly[index5] * 5 <= min) { 33 | index5 += 1; 34 | } 35 | } 36 | return ugly[n]; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/ValidateBST.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking; 2 | 3 | import org.wcong.test.algorithm.tree.BinaryTree; 4 | 5 | /** 6 | * Created by wcong on 2017/4/14. 7 | */ 8 | public class ValidateBST { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | static class Result { 15 | boolean validate = true; 16 | int maxForNow = Integer.MIN_VALUE; 17 | } 18 | 19 | public static boolean validate(BinaryTree.Node root) { 20 | if (root == null) { 21 | return true; 22 | } 23 | Result result = new Result(); 24 | validate(root, result); 25 | return result.validate; 26 | } 27 | 28 | public static void validate(BinaryTree.Node root, Result result) { 29 | if (root == null) { 30 | return; 31 | } 32 | if (root.left != null) { 33 | validate(root.left, result); 34 | } 35 | if (!result.validate) { 36 | return; 37 | } 38 | if (result.maxForNow > root.key) { 39 | result.validate = false; 40 | return; 41 | } 42 | result.maxForNow = root.key; 43 | validate(root.right, result); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/dp/UniqueBinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.dp; 2 | 3 | /** 4 | * test for binary search tree and dynamic programing 5 | *

6 | * // Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 7 | *

8 | * // For example, 9 | * // Given n = 3, there are a total of 5 unique BST's. 10 | *

11 | * // 1 3 3 2 1 12 | * // \ / / / \ \ 13 | * // 3 2 1 1 3 2 14 | * // / / \ \ 15 | * // 2 1 2 3 16 | * Created by wcong on 2017/3/27. 17 | */ 18 | public class UniqueBinarySearchTree { 19 | 20 | public static void main(String[] args) { 21 | 22 | } 23 | 24 | 25 | public static int binarySearchTree(int n) { 26 | if (n <= 0) { 27 | return 0; 28 | } 29 | int[] rangeNum = new int[n]; 30 | rangeNum[0] = 1; 31 | rangeNum[1] = 1; 32 | for (int i = 2; i <= n; i++) { 33 | for (int j = 1; j <= i; j++) { 34 | rangeNum[i] += rangeNum[j - 1] * rangeNum[j - i]; 35 | } 36 | } 37 | return rangeNum[n]; 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/sort_and_search/MergeSorted.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.sort_and_search; 2 | 3 | /** 4 | * you are given two sorted arrays A and B 5 | * where A has a large enough buffer at the end to hold b 6 | * write a method merge B into A in sorted order 7 | * 8 | * @author wcong 9 | * @since 19/04/2017 10 | */ 11 | public class MergeSorted { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | public static void merge(int[] array1, int[] array2) { 18 | int length = array1.length + array2.length; 19 | int array1Index = array1.length - 1; 20 | int array2Index = array2.length - 1; 21 | for (int j = length - 1; j >= 0; j--) { 22 | if (array1Index < 0 && array2Index < 0) { 23 | break; 24 | } else if (array1Index < 0) { 25 | array1[j] = array2[array2Index]; 26 | array2Index -= 1; 27 | } else if (array2Index < 0) { 28 | array1[j] = array1[array1Index]; 29 | array1Index -= 1; 30 | } else { 31 | if (array1[array1Index] < array2[array2Index]) { 32 | array1[j] = array2[array2Index]; 33 | array2Index -= 1; 34 | } else { 35 | array1[j] = array1[array1Index]; 36 | array1Index -= 1; 37 | } 38 | } 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/ReplaceBlank.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for string 7 | * give a string replace all blank by %20 8 | * Created by wcong on 2017/3/23. 9 | */ 10 | public class ReplaceBlank { 11 | 12 | public static void main(String[] args) { 13 | Assert.isTrue(replaceString("abs as das ").equals("abs%20as%20%20das%20")); 14 | } 15 | 16 | public static String replaceString(String oldString) { 17 | int length = oldString.length(); 18 | for (int i = 0; i < oldString.length(); i++) { 19 | if (oldString.charAt(i) == ' ') { 20 | length += 2; 21 | } 22 | } 23 | char[] newChar = new char[length]; 24 | int newCharIndex = 0; 25 | for (int i = 0; i < oldString.length(); i++) { 26 | if (oldString.charAt(i) == ' ') { 27 | newChar[newCharIndex++] = '%'; 28 | newChar[newCharIndex++] = '2'; 29 | newChar[newCharIndex++] = '0'; 30 | } else { 31 | newChar[newCharIndex++] = oldString.charAt(i); 32 | } 33 | } 34 | return new String(newChar); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/guice/MapBinderTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.guice; 2 | 3 | import com.google.inject.AbstractModule; 4 | import com.google.inject.Guice; 5 | import com.google.inject.Inject; 6 | import com.google.inject.Injector; 7 | import com.google.inject.multibindings.MapBinder; 8 | 9 | import java.util.Map; 10 | 11 | /** 12 | * @author wcong 13 | * @since 2016/10/31 14 | */ 15 | public class MapBinderTest { 16 | public static void main(String[] args) { 17 | Injector injector = Guice.createInjector(new AbstractModule() { 18 | @Override 19 | protected void configure() { 20 | MapBinder mapBinder = MapBinder.newMapBinder(binder(), String.class, String.class); 21 | mapBinder.addBinding("hello").toInstance("world"); 22 | binder().bind(MapContainer.class).asEagerSingleton(); 23 | } 24 | }); 25 | MapContainer mapContainer = injector.getInstance(MapContainer.class); 26 | System.out.println(mapContainer.toString()); 27 | } 28 | 29 | public static class MapContainer { 30 | 31 | private Map data; 32 | 33 | @Inject 34 | public MapContainer(Map data) { 35 | this.data = data; 36 | } 37 | 38 | public String toString() { 39 | return data.toString(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/sort_and_search/SparseSearch.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.sort_and_search; 2 | 3 | /** 4 | * given a sorted array of string that is interspersed with empty string 5 | * write a method to find the location of the string 6 | * 7 | * @author wcong 8 | * @since 19/04/2017 9 | */ 10 | public class SparseSearch { 11 | 12 | public static void main(String[] args) { 13 | 14 | } 15 | 16 | public static int index(String[] array, String target) { 17 | return index(array, target, 0, array.length); 18 | } 19 | 20 | private static int index(String[] array, String target, int start, int end) { 21 | if (start > end) { 22 | return -1; 23 | } 24 | int middle = start + (end - start) / 2; 25 | if (array[middle].length() == 0) { 26 | int leftIndex = index(array, target, start, middle - 1); 27 | if (leftIndex > 0) { 28 | return leftIndex; 29 | } 30 | return index(array, target, middle + 1, end); 31 | } else { 32 | if (target.equals(array[middle])) { 33 | return middle; 34 | } else if (target.compareTo(array[middle]) < 0) { 35 | return index(array, target, start, middle - 1); 36 | } else { 37 | return index(array, target, middle + 1, end); 38 | } 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/binary/TwoDifferentNumInArray.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.binary; 2 | 3 | /** 4 | * test for bit operation and array 5 | * give a array two of the numbers appears only once others all appears twice,find the tow number 6 | * Created by wcong on 2017/3/28. 7 | */ 8 | public class TwoDifferentNumInArray { 9 | 10 | public static void main(String[] args) { 11 | twoNumbers(new int[]{1, 2, 3, 4, 5, 6, 4, 3, 2, 5}); 12 | } 13 | 14 | 15 | public static int[] twoNumbers(int[] array) { 16 | if (array == null || array.length < 2) { 17 | return null; 18 | } 19 | int result = 0; 20 | for (int num : array) { 21 | result ^= num; 22 | } 23 | int bitIndex = 0; 24 | while (result != 0) { 25 | if ((result & 1) != 0) { 26 | break; 27 | } 28 | result = result >> 1; 29 | bitIndex += 1; 30 | } 31 | int num1 = 0; 32 | int num2 = 0; 33 | for (int num : array) { 34 | if (((num >> bitIndex) & 1) == 0) { 35 | num1 ^= num; 36 | } else { 37 | num2 ^= num; 38 | } 39 | } 40 | return new int[]{num1, num2}; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/linked_list/ReversLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.linked_list; 2 | 3 | import org.springframework.util.Assert; 4 | import org.wcong.test.algorithm.jzoffer.util.BuildLinkedList; 5 | import org.wcong.test.algorithm.jzoffer.util.LinkedList; 6 | import org.wcong.test.algorithm.jzoffer.util.LinkedListNode; 7 | 8 | /** 9 | * test for linked list 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class ReversLinkedList { 15 | 16 | public static void main(String[] args) { 17 | LinkedList list = BuildLinkedList.makeOne(new int[] { 1, 2, 3, 4, 5, 6, 7 }); 18 | LinkedList reverseList = BuildLinkedList.makeOne(new int[] { 7, 6, 5, 4, 3, 2, 1 }); 19 | Assert.isTrue(reverse(list).equal(reverseList)); 20 | } 21 | 22 | public static LinkedList reverse(LinkedList linkedList) { 23 | if (linkedList == null || linkedList.root == null) { 24 | return linkedList; 25 | } 26 | LinkedListNode before = null; 27 | LinkedListNode node = linkedList.root; 28 | LinkedListNode next = node.next; 29 | node.next = null; 30 | while (next != null) { 31 | before = node; 32 | node = next; 33 | next = next.next; 34 | node.next = before; 35 | } 36 | linkedList.root = node; 37 | return linkedList; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/LongestCommonSubSequence.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * give two string a,b find the longest common sub sequence 5 | * Created by wcong on 2017/3/31. 6 | */ 7 | public class LongestCommonSubSequence { 8 | 9 | public static void main(String[] args) { 10 | 11 | } 12 | 13 | public static int longest(String a, String b) { 14 | int[][] result = new int[a.length() + 1][b.length() + 1]; 15 | if (a.charAt(0) == b.charAt(0)) { 16 | result[1][1] = 1; 17 | } 18 | for (int i = 0; i < b.length(); i++) { 19 | if (a.charAt(0) == b.charAt(i)) { 20 | result[1][i + 1] = 1; 21 | } 22 | } 23 | for (int i = 1; i < a.length(); i++) { 24 | for (int j = 0; j < b.length(); j++) { 25 | if (a.charAt(i) == b.charAt(j)) { 26 | result[i][j] = result[i - 1][j] + 1; 27 | } 28 | } 29 | } 30 | int max = 0; 31 | for (int i = 0; i < a.length(); i++) { 32 | for (int j = 0; j < b.length(); j++) { 33 | if (result[i][j] > max) { 34 | max = result[i][j]; 35 | } 36 | } 37 | } 38 | return max; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/linked_list/LastKNodeInLinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.linked_list; 2 | 3 | import org.springframework.util.Assert; 4 | import org.wcong.test.algorithm.jzoffer.util.BuildLinkedList; 5 | import org.wcong.test.algorithm.jzoffer.util.LinkedList; 6 | import org.wcong.test.algorithm.jzoffer.util.LinkedListNode; 7 | 8 | /** 9 | * test for linked list and exceptions and continue thinking and two pointers 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class LastKNodeInLinkedList { 15 | 16 | public static void main(String[] args) { 17 | LinkedList one = BuildLinkedList.makeOne(new int[] { 1, 2, 3, 4, 5 }); 18 | Assert.isTrue(lastKNode(one, 6) == null); 19 | Assert.isTrue(lastKNode(one, 3).value == 3); 20 | } 21 | 22 | public static LinkedListNode lastKNode(LinkedList linkedList, int k) { 23 | if (linkedList == null || linkedList.root == null || k < 1) { 24 | return null; 25 | } 26 | LinkedListNode lastKNode = linkedList.root; 27 | int count = 1; 28 | LinkedListNode node = linkedList.root; 29 | while (node.next != null) { 30 | count += 1; 31 | node = node.next; 32 | if (count > k) { 33 | lastKNode = lastKNode.next; 34 | } 35 | } 36 | return count < k ? null : lastKNode; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/dp_divide_conque/Parenthesis.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.dp_divide_conque; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | /** 7 | * implement an algorithm to print all valid combinations of n pairs of parenthesis 8 | * 9 | * @author wcong 10 | * @since 19/04/2017 11 | */ 12 | public class Parenthesis { 13 | 14 | public List parenthesis(int n) { 15 | String[] combinations = new String[n + 1]; 16 | combinations[0] = ""; 17 | for (int i = 1; i <= n; i++) { 18 | StringBuilder stringBuilder = new StringBuilder(i * 2); 19 | for (int j = 0; j < i; j++) { 20 | stringBuilder.append("("); 21 | } 22 | for (int j = 0; j < i; j++) { 23 | stringBuilder.append(")"); 24 | } 25 | combinations[i] = stringBuilder.toString(); 26 | } 27 | List result = new LinkedList<>(); 28 | parenthesisCombination(combinations, "", n, result); 29 | return result; 30 | } 31 | 32 | private void parenthesisCombination(String[] combinations, String prefix, int remain, List result) { 33 | if (remain == 0) { 34 | result.add(prefix); 35 | } 36 | for (int i = 1; i <= remain; i++) { 37 | parenthesisCombination(combinations, prefix + combinations[i], remain - 1, result); 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/SortedArraySumTarget.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | import java.util.Arrays; 6 | 7 | /** 8 | * test for array and tow pointer 9 | * give a sorted array return two num sum to a target 10 | * for example 1,2,4,7,11,15 target 12 11 | * Created by wcong on 2017/3/28. 12 | */ 13 | public class SortedArraySumTarget { 14 | 15 | public static void main(String[] args) { 16 | Assert.isTrue(Arrays.equals(sumTarget(new int[]{1, 2, 4, 7, 11, 15}, 12), new int[]{0, 4})); 17 | Assert.isTrue(Arrays.equals(sumTarget(new int[]{1, 2, 4, 7, 11, 15}, 11), new int[]{2, 3})); 18 | } 19 | 20 | public static int[] sumTarget(int[] array, int target) { 21 | if (array == null || array.length < 2) { 22 | return null; 23 | } 24 | int start = 0; 25 | int end = array.length - 1; 26 | int sum; 27 | while (start < end) { 28 | sum = array[start] + array[end]; 29 | if (sum == target) { 30 | return new int[]{start, end}; 31 | } else if (sum > target) { 32 | end -= 1; 33 | } else { 34 | start += 1; 35 | } 36 | } 37 | return null; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/javaformat/IScannerTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.javaformat; 2 | 3 | import org.eclipse.jdt.core.ToolFactory; 4 | import org.eclipse.jdt.core.compiler.IScanner; 5 | import org.eclipse.jdt.core.compiler.InvalidInputException; 6 | 7 | import java.io.IOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.Path; 10 | import java.nio.file.Paths; 11 | 12 | /** 13 | * test for IScanner 14 | * Created by wcong on 2016/11/21. 15 | */ 16 | public class IScannerTest { 17 | public static void main(String[] args) throws IOException, InvalidInputException { 18 | IScanner scanner = ToolFactory.createScanner(true, true, true, "1.8"); 19 | Path path = Paths.get("src/main/java/org/wcong/test/guice/GuiceTest.java"); 20 | String text = new String(Files.readAllBytes(path)); 21 | int textLength = text.length(); 22 | scanner.setSource(text.toCharArray()); 23 | while (scanner.getCurrentTokenEndPosition() < textLength - 1) { 24 | scanner.getNextToken(); 25 | int charI0 = scanner.getCurrentTokenStartPosition(); 26 | // Get string, possibly with Unicode escapes. 27 | String originalTokText = text.substring(charI0, scanner.getCurrentTokenEndPosition() + 1); 28 | System.out.println(originalTokText); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/sort_and_search/GroupAnagrams.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.sort_and_search; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * write a method to sort an array of strings so that all the anagrams are next to each other 11 | * 12 | * @author wcong 13 | * @since 19/04/2017 14 | */ 15 | public class GroupAnagrams { 16 | 17 | public static void main(String[] args) { 18 | 19 | } 20 | 21 | public static String[] groupAnagrams(String[] array) { 22 | Map> map = new HashMap<>(); 23 | for (String solo : array) { 24 | char[] charArray = solo.toCharArray(); 25 | Arrays.sort(charArray); 26 | String sortedArray = new String(charArray); 27 | List stringList = map.get(sortedArray); 28 | if (stringList == null) { 29 | stringList = new LinkedList<>(); 30 | map.put(sortedArray, stringList); 31 | } 32 | stringList.add(solo); 33 | } 34 | String[] anagrams = new String[array.length]; 35 | int index = 0; 36 | for (List stringList : map.values()) { 37 | for (String solo : stringList) { 38 | anagrams[index] = solo; 39 | index += 1; 40 | } 41 | } 42 | return anagrams; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/ReArrangeOddAndEven.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | import java.util.Arrays; 6 | 7 | /** 8 | * test for array and odd and even and two pointers 9 | * give an array of integer rearrange odd number to left and even number to left 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class ReArrangeOddAndEven { 15 | 16 | public static void main(String[] args) { 17 | Assert.isTrue( 18 | Arrays.equals(rearrange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }), new int[] { 1, 7, 3, 5, 4, 6, 2, 8 })); 19 | } 20 | 21 | public static int[] rearrange(int[] array) { 22 | if (array == null || array.length == 0) { 23 | return array; 24 | } 25 | int leftEvenIndex = 0; 26 | int rightOddIndex = array.length - 1; 27 | while (leftEvenIndex < rightOddIndex) { 28 | if ((array[leftEvenIndex] & 1) != 0) { 29 | leftEvenIndex += 1; 30 | continue; 31 | } 32 | if ((array[rightOddIndex] & 1) == 0) { 33 | rightOddIndex -= 1; 34 | continue; 35 | } 36 | int temp = array[leftEvenIndex]; 37 | array[leftEvenIndex] = array[rightOddIndex]; 38 | array[rightOddIndex] = temp; 39 | leftEvenIndex += 1; 40 | rightOddIndex -= 1; 41 | } 42 | return array; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/stack/TwoStackForQueue.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.stack; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | import java.util.Stack; 6 | 7 | /** 8 | * test for stack and queue 9 | * implement a queue bu two stack 10 | * Created by wcong on 2017/3/24. 11 | */ 12 | public class TwoStackForQueue { 13 | 14 | 15 | public static void main(String[] args) { 16 | StackQueue stackQueue = new StackQueue(); 17 | stackQueue.push(1); 18 | stackQueue.push(2); 19 | stackQueue.push(3); 20 | stackQueue.push(4); 21 | Assert.isTrue(stackQueue.pop() == 1); 22 | Assert.isTrue(stackQueue.pop() == 2); 23 | Assert.isTrue(stackQueue.pop() == 3); 24 | Assert.isTrue(stackQueue.pop() == 4); 25 | } 26 | 27 | 28 | public static class StackQueue { 29 | 30 | private Stack in = new Stack<>(); 31 | 32 | private Stack out = new Stack<>(); 33 | 34 | public void push(int num) { 35 | in.push(num); 36 | } 37 | 38 | public int pop() { 39 | if (out.isEmpty()) { 40 | while (!in.isEmpty()) { 41 | out.push(in.pop()); 42 | } 43 | } 44 | return out.pop(); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/MatrixChainMultiplication.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * we are given a sequence of matrix to to multiplied 5 | * and we wish to computer the product 6 | * TODO 7 | * Created by wcong on 2017/3/30. 8 | */ 9 | public class MatrixChainMultiplication { 10 | 11 | static class MatrixSymbol { 12 | int row = 1; 13 | int column = 1; 14 | 15 | public MatrixSymbol mulitply(MatrixSymbol other) { 16 | MatrixSymbol matrixSymbol = new MatrixSymbol(); 17 | matrixSymbol.row = row; 18 | matrixSymbol.column = other.column; 19 | return matrixSymbol; 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | 25 | } 26 | 27 | public static int multipiedNum(MatrixSymbol[] matrixArray) { 28 | int[] multipliedNum = new int[matrixArray.length + 1]; 29 | MatrixSymbol[] multipliedMetrix = new MatrixSymbol[matrixArray.length + 1]; 30 | for (int i = 1; i < matrixArray.length; i++) { 31 | int max = -1; 32 | int lastMultiplied = 0; 33 | MatrixSymbol lastMatrixSybmol = new MatrixSymbol(); 34 | for (int j = i; j > 0; j--) { 35 | lastMultiplied += matrixArray[j].row; 36 | } 37 | } 38 | return 0; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/ShortestPalindrome.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | /** 4 | * Given a string S 5 | * you are allowed to convert it to a palindrome by adding characters in front of it. 6 | * Find and return the shortest palindrome you can find by performing this transformation. 7 | * For example: 8 | * Given "aacecaaa", return "aaacecaaa". 9 | * Given "abcd", return "dcbabcd". 10 | * 11 | * @author wcong 12 | * @since 07/06/2017 13 | */ 14 | public class ShortestPalindrome { 15 | 16 | public String shortestPalindromeBruteForce(String s) { 17 | StringBuilder insert = new StringBuilder(); 18 | int end = s.length() - 1; 19 | while (end > 0) { 20 | if (isPalindrome(s, 0, end)) { 21 | break; 22 | } else { 23 | insert.append(s.charAt(end)); 24 | end -= 1; 25 | } 26 | } 27 | return insert + s; 28 | } 29 | 30 | private boolean isPalindrome(String s, int start, int end) { 31 | while (start <= end) { 32 | if (s.charAt(start) == s.charAt(end)) { 33 | start += 1; 34 | end -= 1; 35 | } else { 36 | return false; 37 | } 38 | } 39 | return true; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/string/LongestValidParenthesesTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.string; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.string.LongestValidParentheses; 6 | 7 | /** 8 | * @author wcong 9 | * @since 03/06/2017 10 | */ 11 | public class LongestValidParenthesesTest { 12 | 13 | @Test 14 | public void testLongestValidParentheses() { 15 | String s1 = "(()"; 16 | String s2 = ")()())"; 17 | String s3 = ")()()))()()()(())"; 18 | String s4 = "()(()"; 19 | String s5 = "(()(((()"; 20 | LongestValidParentheses solution = new LongestValidParentheses(); 21 | Assert.assertTrue(solution.longestValidParenthesesBruteForce(s1) == 2); 22 | Assert.assertTrue(solution.longestValidParenthesesBruteForce(s2) == 4); 23 | Assert.assertTrue(solution.longestValidParenthesesBruteForce(s3) == 10); 24 | Assert.assertTrue(solution.longestValidParenthesesGreed(s1) == 2); 25 | Assert.assertTrue(solution.longestValidParenthesesGreed(s2) == 4); 26 | Assert.assertTrue(solution.longestValidParenthesesGreed(s3) == 10); 27 | Assert.assertTrue(solution.longestValidParenthesesGreed(s4) == 2); 28 | Assert.assertTrue(solution.longestValidParenthesesGreed(s5) == 2); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/tree/InvertBinaryTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.tree; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * as the name says 7 | * Created by wcong on 2017/4/1. 8 | */ 9 | public class InvertBinaryTree { 10 | 11 | public static void main(String[] args) { 12 | 13 | } 14 | 15 | public static void invert(TreeNode root) { 16 | if (root == null) { 17 | return; 18 | } 19 | TreeNode temp = root.left; 20 | root.left = root.right; 21 | root.right = temp; 22 | invert(root.left); 23 | invert(root.right); 24 | } 25 | 26 | public static void invertLoop(TreeNode root) { 27 | if (root == null) { 28 | return; 29 | } 30 | Stack treeNodeStack = new Stack<>(); 31 | treeNodeStack.push(root); 32 | while (!treeNodeStack.isEmpty()) { 33 | TreeNode treeNode = treeNodeStack.pop(); 34 | TreeNode temp = treeNode.left; 35 | treeNode.left = treeNode.right; 36 | treeNode.right = temp; 37 | if (treeNode.left != null) { 38 | treeNodeStack.push(treeNode.left); 39 | } 40 | if (treeNode.right != null) { 41 | treeNodeStack.push(treeNode.right); 42 | } 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/array/NumbersOfIslands.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.array; 2 | 3 | /** 4 | * Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. 5 | * An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. 6 | * You may assume all four edges of the grid are all surrounded by water. 7 | * 8 | * @author wcong 9 | * @since 09/05/2017 10 | */ 11 | public class NumbersOfIslands { 12 | public int numIslandsBruteForce(char[][] grid) { 13 | int nums = 0; 14 | for (int row = 0; row < grid.length; row++) { 15 | for (int column = 0; column < grid[row].length; column++) { 16 | if (grid[row][column] == '1') { 17 | nums += 1; 18 | elimateLand(grid, row, column); 19 | } 20 | } 21 | } 22 | return nums; 23 | } 24 | 25 | private void elimateLand(char[][] grid, int row, int column) { 26 | if (grid[row][column] == '0') { 27 | return; 28 | } 29 | grid[row][column] = '0'; 30 | if (row > 0) { 31 | elimateLand(grid, row - 1, column); 32 | } 33 | if (column > 0) { 34 | elimateLand(grid, row, column - 1); 35 | } 36 | if (row < grid.length - 1) { 37 | elimateLand(grid, row + 1, column); 38 | } 39 | if (column < grid[row].length - 1) { 40 | elimateLand(grid, row, column + 1); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/linked_list/Palindrome.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.linked_list; 2 | 3 | import org.wcong.test.algorithm.basic.LinkedList; 4 | 5 | /** 6 | * implement a function to check if a linked list is palindrome 7 | * 8 | * @author wcong 9 | * @since 17/04/2017 10 | */ 11 | public class Palindrome { 12 | 13 | public static void main(String[] args) { 14 | 15 | } 16 | 17 | static class Result { 18 | LinkedList.Node node; 19 | 20 | boolean result = true; 21 | 22 | } 23 | 24 | public static boolean isPalindrome(LinkedList.Node root) { 25 | int length = length(root); 26 | return true; 27 | } 28 | 29 | private static Result judge(LinkedList.Node node, int length) { 30 | if (length <= 0) { 31 | Result result = new Result(); 32 | result.node = node.next; 33 | return result; 34 | } else if (length == 1) { 35 | Result result = new Result(); 36 | result.node = node; 37 | return result; 38 | } 39 | Result result = judge(node, length - 2); 40 | if (!result.result) { 41 | return result; 42 | } 43 | result.result = node.value == result.node.value; 44 | result.node = node.next; 45 | return result; 46 | } 47 | 48 | public static int length(LinkedList.Node root) { 49 | int length = 0; 50 | while (root != null) { 51 | length += 1; 52 | root = root.next; 53 | } 54 | return length; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/CreateMaximumNumberTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.CreateMaximumNumber; 6 | 7 | import java.util.Arrays; 8 | 9 | /** 10 | * @author wcong 11 | * @since 13/06/2017 12 | */ 13 | public class CreateMaximumNumberTest { 14 | 15 | @Test 16 | public void testMaxNumber() { 17 | 18 | CreateMaximumNumber solution = new CreateMaximumNumber(); 19 | int[] e3nums1 = new int[]{3, 9}; 20 | int[] e3nums2 = new int[]{8, 9}; 21 | int[] e3result = new int[]{9, 8, 9}; 22 | Assert.assertTrue(Arrays.equals(solution.maxNumberGreed(e3nums1, e3nums2, 3), e3result)); 23 | int[] e1nums1 = new int[]{3, 4, 6, 5}; 24 | int[] e1nums2 = new int[]{9, 1, 2, 5, 8, 3}; 25 | int[] e1result = new int[]{9, 8, 6, 5, 3}; 26 | Assert.assertTrue(Arrays.equals(solution.maxNumberGreed(e1nums1, e1nums2, 5), e1result)); 27 | int[] e2nums1 = new int[]{6, 7}; 28 | int[] e2nums2 = new int[]{6, 0, 4}; 29 | int[] e2result = new int[]{6, 7, 6, 0, 4}; 30 | Assert.assertTrue(Arrays.equals(solution.maxNumberGreed(e2nums1, e2nums2, 5), e2result)); 31 | try { 32 | throw new NoClassDefFoundError(); 33 | } catch (Error e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | 5 | /** 6 | * Given n non-negative integers a1, a2, ..., an, 7 | * where each represents a point at coordinate (i, ai). 8 | * n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). 9 | * Find two lines, which together with x-axis forms a container, such that the container contains the most water. 10 | * Note: You may not slant the container and n is at least 2. 11 | * Created by hzwangcong on 2017/3/1. 12 | */ 13 | public class ContainerWithMostWater { 14 | 15 | public static void main(String[] args) { 16 | Assert.isTrue(maxArea(new int[]{1, 2}) == 1); 17 | Assert.isTrue(maxArea(new int[]{1, 2, 1}) == 2); 18 | Assert.isTrue(maxArea(new int[]{3, 2, 1, 3}) == 9); 19 | } 20 | 21 | /** 22 | * go to next element 23 | * second height 24 | */ 25 | public static int maxArea(int[] height) { 26 | int left = 0, right = height.length - 1; 27 | int maxArea = 0; 28 | 29 | while (left < right) { 30 | maxArea = Math.max(maxArea, Math.min(height[left], height[right]) 31 | * (right - left)); 32 | if (height[left] < height[right]) 33 | left++; 34 | else 35 | right--; 36 | } 37 | 38 | return maxArea; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/PrintNearly.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | /** 4 | * Consider the problem of neatly printing a paragraph with a monospaced font (all 5 | * characters having the same width) on a printer. 6 | * The input text is a sequence of n words of lengths l1; l2; : : : ; ln, measured in characters. 7 | * We want to print this paragraph neatly on a number of lines that hold a maximum of M characters each. 8 | * Our criterion of “neatness” is as follows. If a given line contains words i through j , 9 | * where i <= j , and we leave exactly one space between words, the number of extra 10 | * We wish to minimize the left space to right. 11 | * Created by wcong on 2017/3/31. 12 | */ 13 | public class PrintNearly { 14 | 15 | public static void main(String[] args) { 16 | 17 | } 18 | 19 | public static int paintNum(int[] array, int m) { 20 | int[][] result = new int[array.length + 1][array.length + 1]; 21 | for (int i = 0; i < array.length; i++) { 22 | for (int j = 0; j <= i; j++) { 23 | if (array[i] + result[i][j] <= m) { 24 | result[i + 1][j + 1] = result[i][j + 1] + array[i]; 25 | } else { 26 | result[i + 1][j + 1] = result[i - 1][j] > result[i][j - 1] ? result[i - 1][j] : result[i][j - 1]; 27 | } 28 | } 29 | } 30 | return result[array.length][array.length]; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /annotation/src/main/java/org/wcong/test/util/ElementUtils.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.util; 2 | 3 | import javax.annotation.processing.Filer; 4 | import javax.annotation.processing.Messager; 5 | import javax.lang.model.element.Element; 6 | import javax.lang.model.element.PackageElement; 7 | import javax.tools.Diagnostic; 8 | import javax.tools.JavaFileObject; 9 | import java.io.IOException; 10 | import java.io.Writer; 11 | 12 | /** 13 | * Created by wcong on 2016/12/6. 14 | */ 15 | public class ElementUtils { 16 | public static String getPackageName(Element element) { 17 | while (true) { 18 | Element enclosingElement = element.getEnclosingElement(); 19 | if (enclosingElement instanceof PackageElement) { 20 | return ((PackageElement) enclosingElement).getQualifiedName().toString(); 21 | } 22 | element = enclosingElement; 23 | } 24 | } 25 | 26 | public static void writeFile(Filer filer, Messager messager, String className, String text, Element element) { 27 | try { 28 | JavaFileObject sourceFile = filer.createSourceFile(className, element); 29 | Writer writer = sourceFile.openWriter(); 30 | try { 31 | writer.write(text); 32 | } finally { 33 | writer.close(); 34 | } 35 | } catch (IOException e) { 36 | messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/string/LongestSubStringContainKDistinctWord.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.string; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * give a string find the longest sub string that contains k distinct word 7 | * for example cecba k =2 longest sub string is ece 8 | * Created by wcong on 2017/4/1. 9 | */ 10 | public class LongestSubStringContainKDistinctWord { 11 | 12 | public static void main(String[] args) { 13 | Assert.isTrue(subString("cecba", 2).equals("cec")); 14 | } 15 | 16 | public static String subString(String a, int k) { 17 | int longestStart = 0; 18 | int longestEnd = 0; 19 | int start = 0; 20 | int distinct = 0; 21 | for (int i = 0; i < a.length(); i++) { 22 | if (!a.substring(start, i).contains(a.substring(i, i + 1))) { 23 | distinct += 1; 24 | } 25 | if (distinct == k && i - start > longestEnd - longestStart) { 26 | longestEnd = i; 27 | longestStart = start; 28 | } else if (distinct > k) { 29 | while (start <= i) { 30 | start = start + 1; 31 | if (!a.substring(start + 1, i + 1).contains(a.substring(start, start + 1))) { 32 | break; 33 | } 34 | } 35 | } 36 | } 37 | return a.substring(longestStart, longestEnd + 1); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/tree/ChangeBinaryTreeToDQueue.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.tree; 2 | 3 | import org.wcong.test.algorithm.jzoffer.util.BuildTree; 4 | import org.wcong.test.algorithm.jzoffer.util.Tree; 5 | import org.wcong.test.algorithm.jzoffer.util.TreeNode; 6 | 7 | /** 8 | * test for tree and dqueue TODO 9 | * give a binary tree change it to dqueue 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class ChangeBinaryTreeToDQueue { 15 | 16 | 17 | public static void main(String[] args) { 18 | changeToDQueue(BuildTree.buildOne(new int[]{4, 2, 6, 1, 3, 5, 7})); 19 | } 20 | 21 | public static TreeNode changeToDQueue(Tree tree) { 22 | TreeNode treeNode = changeToDQueue(tree.root, null); 23 | while (treeNode.left != null) { 24 | treeNode = treeNode.left; 25 | } 26 | return treeNode; 27 | } 28 | 29 | private static TreeNode changeToDQueue(TreeNode treeNode, TreeNode lastInQueue) { 30 | if (treeNode.left != null) { 31 | lastInQueue = changeToDQueue(treeNode.left, lastInQueue); 32 | } 33 | if (lastInQueue != null) { 34 | lastInQueue.right = treeNode; 35 | treeNode.left = lastInQueue; 36 | } 37 | lastInQueue = treeNode; 38 | if (treeNode.right != null) { 39 | lastInQueue = changeToDQueue(treeNode.right, lastInQueue); 40 | } 41 | return lastInQueue; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/linked_list/ComplexListNodeCopy.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.linked_list; 2 | 3 | import org.wcong.test.algorithm.jzoffer.util.ComplexListNode; 4 | 5 | /** 6 | * test for linked list 7 | * give a complex linked list copy it 8 | * 9 | * @author wcong 10 | * @since 2017/3/26 11 | */ 12 | public class ComplexListNodeCopy { 13 | 14 | public static ComplexListNode copyComplexList(ComplexListNode head) { 15 | if (head == null) { 16 | return null; 17 | } 18 | ComplexListNode node = head; 19 | while (node != null) { 20 | ComplexListNode copy = new ComplexListNode(); 21 | copy.value = node.value; 22 | copy.next = node.next; 23 | node.next = copy; 24 | node = copy.next; 25 | } 26 | node = head; 27 | while (node != null) { 28 | ComplexListNode copy = node.next; 29 | if (node.other != null) { 30 | copy.other = node.other.next; 31 | } 32 | node = copy.next; 33 | } 34 | node = head; 35 | ComplexListNode root = head.next; 36 | while (node != null) { 37 | ComplexListNode copy = node.next; 38 | node.next = copy.next; 39 | node = copy.next; 40 | if( copy.next != null ) { 41 | copy.next = copy.next.next; 42 | } 43 | } 44 | return root; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/ApplicationListenerTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.context.ApplicationEvent; 4 | import org.springframework.context.ApplicationListener; 5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.stereotype.Component; 8 | import org.springframework.stereotype.Service; 9 | 10 | /** 11 | * @author wcong 12 | * @since 16/1/15 13 | */ 14 | @Configuration 15 | public class ApplicationListenerTest { 16 | 17 | public static void main(String[] args) { 18 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 19 | annotationConfigApplicationContext.register(ApplicationListenerTest.class); 20 | annotationConfigApplicationContext.refresh(); 21 | PrintSomething printSomething = annotationConfigApplicationContext.getBean(PrintSomething.class); 22 | printSomething.print(); 23 | } 24 | 25 | public interface PrintSomething { 26 | void print(); 27 | } 28 | 29 | @Service 30 | public static class PrintHello implements PrintSomething { 31 | 32 | public void print() { 33 | System.out.println("hello"); 34 | } 35 | } 36 | 37 | @Component 38 | public static class ApplicationListenerComponent implements ApplicationListener { 39 | 40 | public void onApplicationEvent(ApplicationEvent event) { 41 | System.out.println(event); 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/dagger/CircleBindingTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.dagger; 2 | 3 | import dagger.Binds; 4 | import dagger.Component; 5 | import dagger.Module; 6 | import dagger.Provides; 7 | 8 | import javax.inject.Inject; 9 | import javax.inject.Singleton; 10 | 11 | /** 12 | * @author wcong 13 | * @since 2016/11/7 14 | */ 15 | public class CircleBindingTest { 16 | public static void main(String[] args) { 17 | MyComponent myComponent = DaggerCircleBindingTest_MyComponent.builder().build(); 18 | myComponent.getExportClass().print(); 19 | } 20 | 21 | @Singleton 22 | @Component(modules = { MyBindModule.class, MyProvidesModule.class }) 23 | interface MyComponent { 24 | ExportClass getExportClass(); 25 | } 26 | 27 | static class ExportClass { 28 | private final MyClass myClass; 29 | 30 | 31 | @Inject 32 | public ExportClass(MyClass myClass) { 33 | this.myClass = myClass; 34 | } 35 | 36 | public void print(){ 37 | 38 | } 39 | } 40 | 41 | @Module 42 | static class MyProvidesModule { 43 | @Provides 44 | MyParam getMyParam() { 45 | return new MyParam(); 46 | } 47 | } 48 | 49 | @Module 50 | abstract class MyBindModule { 51 | 52 | @Binds 53 | abstract MyClass myClass(MySubClass a); 54 | } 55 | 56 | static class MyClass { 57 | 58 | } 59 | 60 | static class MySubClass extends MyClass { 61 | 62 | private final MyParam myParam; 63 | 64 | @Inject 65 | public MySubClass(MyParam myParam) { 66 | this.myParam = myParam; 67 | } 68 | } 69 | 70 | static class MyParam { 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/CountInSortedArray.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | /** 4 | * test for array and binary search 5 | * give a sorted array count the target appear num 6 | * Created by wcong on 2017/3/28. 7 | */ 8 | public class CountInSortedArray { 9 | 10 | public static void main(String[] args) { 11 | 12 | } 13 | 14 | private static int countInSortedArray(int[] array, int target) { 15 | int index = index(array, 0, array.length - 1, target); 16 | if (index == 0) { 17 | return 0; 18 | } 19 | int count = 1; 20 | for (int i = index - 1; i > 0; i--) { 21 | if (array[i] == target) { 22 | count += 1; 23 | } else { 24 | break; 25 | } 26 | } 27 | for (int i = index + 1; i < array.length; i++) { 28 | if (array[i] == target) { 29 | count += 1; 30 | } else { 31 | break; 32 | } 33 | } 34 | return count; 35 | } 36 | 37 | public static int index(int[] array, int start, int end, int target) { 38 | if (start < end) { 39 | return -1; 40 | } 41 | int middle = start + (end - start) / 2; 42 | if (array[middle] == target) { 43 | return middle; 44 | } else if (array[middle] > target) { 45 | return index(array, start, middle - 1, target); 46 | } else { 47 | return index(array, middle + 1, end, target); 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/dp/LongestPalindromeSubsequence.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.dp; 2 | 3 | import org.eclipse.core.runtime.Assert; 4 | 5 | /** 6 | * give a string find the longest palindrome sub sequence 7 | * Created by wcong on 2017/3/31. 8 | */ 9 | public class LongestPalindromeSubsequence { 10 | 11 | public static void main(String[] args) { 12 | Assert.isTrue(palindrome("cabdbae") == 5); 13 | } 14 | 15 | public static int palindrome(String a) { 16 | int[][] result = new int[a.length()][a.length()]; 17 | for (int i = 0; i < a.length(); i++) { 18 | result[i][i] = 1; 19 | } 20 | for (int i = 1; i < a.length(); i++) { 21 | for (int j = 0; j < i; j++) { 22 | if (a.charAt(i) == a.charAt(j)) { 23 | if (i - j - 1 == 1) { 24 | result[i][j] = 3; 25 | } else if (i - 1 > 0 && j + 1 < i - 1) { 26 | if (result[i - 1][j + 1] > 0) { 27 | result[i][j] = result[i - 1][j + 1] + 2; 28 | } 29 | } else { 30 | result[i][j] = 1; 31 | } 32 | } 33 | } 34 | } 35 | int max = 1; 36 | for (int i = 0; i < a.length(); i++) { 37 | for (int j = 0; j < a.length(); j++) { 38 | if (result[i][j] > max) { 39 | max = result[i][j]; 40 | } 41 | } 42 | } 43 | return max; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/MultipleTransactionManager.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 7 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | import javax.sql.DataSource; 12 | 13 | /** 14 | * @author wcong 15 | * @since 16/2/23 16 | */ 17 | @Configuration 18 | @EnableTransactionManagement 19 | public class MultipleTransactionManager { 20 | 21 | public static void main(String[] args) { 22 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 23 | annotationConfigApplicationContext.register(MultipleTransactionManager.class); 24 | annotationConfigApplicationContext.refresh(); 25 | } 26 | 27 | 28 | @Bean 29 | public DataSourceTransactionManager getDataSourceTransactionManager() { 30 | return new DataSourceTransactionManager(); 31 | } 32 | 33 | @Bean 34 | public DataSource getDataSource() { 35 | EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder(); 36 | return embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2).build(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/graph/PathWithSum.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.graph; 2 | 3 | import org.wcong.test.algorithm.BinaryTree; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | /** 9 | * you are given a binary tree which every node contains an integer value(positive or negative) 10 | * design an algorithm to count the numbers of path that sum to a given value 11 | * the path does need to start or end at the root or leaf but it must go downwards 12 | * depth first traversal, it can be solved by a algorithm for count subList sum to a target 13 | * 14 | * @author wcong 15 | * @since 18/04/2017 16 | */ 17 | public class PathWithSum { 18 | 19 | public static void main(String[] args) { 20 | 21 | } 22 | 23 | public static int count(BinaryTree.Node root, int target) { 24 | return count(root, 0, target, new HashMap()); 25 | } 26 | 27 | private static int count(BinaryTree.Node root, int sum, int target, Map sumMap) { 28 | if (root == null) { 29 | return 0; 30 | } 31 | sum += root.value; 32 | int left = target - sum; 33 | Integer leftNum = sumMap.get(left); 34 | leftNum = leftNum == null ? 0 : leftNum; 35 | int count = leftNum; 36 | if (sum == target) { 37 | count += 1; 38 | } 39 | 40 | sumMap.put(sum, sumMap.get(sum) == null ? 1 : sumMap.get(sum) + 1); 41 | 42 | int leftCount = count(root.left, sum, target, sumMap); 43 | int rightCount = count(root.right, sum, target, sumMap); 44 | sumMap.put(sum, sumMap.get(sum) - 1); 45 | return count + leftCount + rightCount; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/graph/BinarySequences.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.graph; 2 | 3 | import org.wcong.test.algorithm.BinaryTree; 4 | 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | /** 9 | * give a binary search tree with distinct elements 10 | * print all possible arrays that could led to this tree 11 | * 12 | * @author wcong 13 | * @since 18/04/2017 14 | */ 15 | public class BinarySequences { 16 | 17 | public static void main(String[] args) { 18 | 19 | } 20 | 21 | public static List> sequences(BinaryTree.Node root) { 22 | List> result = new LinkedList<>(); 23 | List path = new LinkedList<>(); 24 | List nextList = new LinkedList<>(); 25 | nextList.add(root); 26 | sequences(nextList, path, result); 27 | return result; 28 | } 29 | 30 | private static void sequences(List nextList, List path, 31 | List> result) { 32 | if (nextList.isEmpty()) { 33 | result.add(path); 34 | return; 35 | } 36 | int i = 0; 37 | for (BinaryTree.Node next : nextList) { 38 | List soloPath = new LinkedList<>(path); 39 | path.add(next); 40 | List nextNextList = new LinkedList<>(nextList); 41 | nextNextList.remove(i); 42 | if (next.left != null) { 43 | nextNextList.add(next.left); 44 | } 45 | if (next.right != null) { 46 | nextNextList.add(next.right); 47 | } 48 | sequences(nextNextList, soloPath, result); 49 | i += 1; 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/array/SortedArraySumTargetSubSerial.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.array; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | /** 8 | * test for array and two pointer 9 | * give a sorted array return all serial sub array sum to target 10 | * for example a array 1,2,3,4,5,6,7,8 target 15 will give {1,2,3,4,5} {4,5,6} {7,8} 11 | * Created by wcong on 2017/3/28. 12 | */ 13 | public class SortedArraySumTargetSubSerial { 14 | 15 | 16 | public static void main(String[] args) { 17 | subSerial(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 15); 18 | } 19 | 20 | public static List subSerial(int[] array, int target) { 21 | if (array == null || array.length < 2) { 22 | return null; 23 | } 24 | List resultList = new LinkedList<>(); 25 | int left = 0, right = 1; 26 | int sum = array[left] + array[right]; 27 | while (right < array.length) { 28 | if (sum > target) { 29 | if (left == right) { 30 | break; 31 | } 32 | sum -= array[left++]; 33 | } else if (sum < target) { 34 | if (right == array.length - 1) { 35 | break; 36 | } 37 | sum += array[++right]; 38 | } 39 | if (sum == target) { 40 | resultList.add(Arrays.copyOfRange(array, left, right + 1)); 41 | sum -= array[left++]; 42 | } 43 | } 44 | return resultList; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/SynchronizedMethod.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * @author wcong 7 | * @since 30/04/2017 8 | */ 9 | public class SynchronizedMethod { 10 | 11 | LinkedList numList = new LinkedList<>(); 12 | 13 | public synchronized Integer get() { 14 | System.out.println("Thread" + Thread.currentThread().getId() + "get the lock and sleep"); 15 | try { 16 | Thread.sleep(5000L); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | return numList.pop(); 21 | } 22 | 23 | public synchronized void push(Integer num) { 24 | System.out.println("Thread" + Thread.currentThread().getId() + "get the lock and sleep"); 25 | try { 26 | Thread.sleep(5000L); 27 | } catch (InterruptedException e) { 28 | e.printStackTrace(); 29 | } 30 | numList.push(num); 31 | } 32 | 33 | public static class MyGetThread extends Thread { 34 | 35 | private SynchronizedMethod synchronizedMethod; 36 | 37 | public MyGetThread(SynchronizedMethod synchronizedMethod) { 38 | this.synchronizedMethod = synchronizedMethod; 39 | } 40 | 41 | public void run() { 42 | System.out.println("get " + synchronizedMethod.get()); 43 | } 44 | } 45 | 46 | public static class MyPutThread extends Thread { 47 | 48 | private SynchronizedMethod synchronizedMethod; 49 | 50 | public MyPutThread(SynchronizedMethod synchronizedMethod) { 51 | this.synchronizedMethod = synchronizedMethod; 52 | } 53 | 54 | public void run() { 55 | synchronizedMethod.push(1); 56 | System.out.println("put 1 "); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/AnnotationTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.cache.CacheManager; 4 | import org.springframework.cache.annotation.Cacheable; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.cache.concurrent.ConcurrentMapCache; 7 | import org.springframework.cache.support.SimpleCacheManager; 8 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.stereotype.Component; 12 | 13 | import java.util.Collections; 14 | 15 | /** 16 | * @author wcong 17 | * @since 15/12/4 18 | */ 19 | @Configuration 20 | @EnableCaching 21 | public class AnnotationTest { 22 | 23 | public static void main(String[] args) { 24 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext( 25 | AnnotationTest.class); 26 | CacheNum cacheNum = annotationConfigApplicationContext.getBean(CacheNum.class); 27 | for (int i = 0; i < 10; i++) { 28 | System.out.println(cacheNum.getNum()); 29 | } 30 | } 31 | 32 | @Bean 33 | public CacheManager cacheManager() { 34 | SimpleCacheManager cacheManager = new SimpleCacheManager(); 35 | cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("num"))); 36 | return cacheManager; 37 | } 38 | 39 | @Component 40 | public static class CacheNum { 41 | int a = 1; 42 | 43 | @Cacheable("num") 44 | public Integer getNum() { 45 | return a++; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/CustomizeAutowiredTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Configuration 10 | public class CustomizeAutowiredTest { 11 | 12 | public static void main(String[] args) { 13 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 14 | annotationConfigApplicationContext.register(CustomizeAutowiredTest.class); 15 | annotationConfigApplicationContext.refresh(); 16 | BeanClass beanClass = annotationConfigApplicationContext.getBean(BeanClass.class); 17 | beanClass.print(); 18 | } 19 | 20 | @Component 21 | public static class BeanClass { 22 | 23 | @MyInject 24 | private FieldClass fieldClass; 25 | 26 | public void print() { 27 | fieldClass.print(); 28 | } 29 | 30 | } 31 | 32 | @Component 33 | public static class FieldClass { 34 | public void print() { 35 | System.out.println("hello world"); 36 | } 37 | } 38 | 39 | @Bean 40 | public AutowiredAnnotationBeanPostProcessor getAutowiredAnnotationBeanPostProcessor() { 41 | AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = new AutowiredAnnotationBeanPostProcessor(); 42 | autowiredAnnotationBeanPostProcessor.setAutowiredAnnotationType(MyInject.class); 43 | return autowiredAnnotationBeanPostProcessor; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/concurrent/Basic.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.concurrent; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | /** 7 | * @author wcong 8 | * @since 29/04/2017 9 | */ 10 | public class Basic { 11 | 12 | public static class MyThread extends Thread { 13 | 14 | private CountDownLatch countDownLatch; 15 | 16 | public MyThread(CountDownLatch countDownLatch) { 17 | this.countDownLatch = countDownLatch; 18 | } 19 | 20 | @Override 21 | public void run() { 22 | try { 23 | countDownLatch.await(); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | System.out.println("I'm running"); 28 | } 29 | } 30 | 31 | public static class MyRunner implements Runnable { 32 | 33 | private CountDownLatch countDownLatch; 34 | 35 | public MyRunner(CountDownLatch countDownLatch) { 36 | this.countDownLatch = countDownLatch; 37 | } 38 | 39 | @Override 40 | public void run() { 41 | try { 42 | countDownLatch.await(); 43 | } catch (InterruptedException e) { 44 | e.printStackTrace(); 45 | } 46 | System.out.println("I'm running too"); 47 | } 48 | } 49 | 50 | public static void main(String[] args) throws InterruptedException { 51 | 52 | CountDownLatch countDownLatch = new CountDownLatch(1); 53 | Thread thread = new MyThread(countDownLatch); 54 | thread.start(); 55 | Thread secondThread = new Thread(new MyRunner(countDownLatch)); 56 | secondThread.start(); 57 | System.out.println("wait for it"); 58 | thread.join(); 59 | countDownLatch.countDown(); 60 | TimeUnit.MILLISECONDS.sleep(1000); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/tree/BinaryTreePostOrderWalkRule.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.tree; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for binary search tree post-order walk analysis 7 | * give a array of integer judge is binary search tree post-order walk 8 | * for example 5,7,6,9,11,10,8 is true,7,4,6,5 is not 9 | * 10 | * @author wcong 11 | * @since 2017/3/26 12 | */ 13 | public class BinaryTreePostOrderWalkRule { 14 | 15 | public static void main(String[] args) { 16 | Assert.isTrue(isBinarySearchTree(new int[]{5, 7, 6, 9, 11, 10, 8})); 17 | Assert.isTrue(!isBinarySearchTree(new int[]{7, 4, 6, 5})); 18 | } 19 | 20 | public static boolean isBinarySearchTree(int[] array) { 21 | if (array == null || array.length == 0) { 22 | return false; 23 | } 24 | return isBinarySearchTree(array, 0, array.length - 1); 25 | } 26 | 27 | private static boolean isBinarySearchTree(int[] array, int start, int end) { 28 | if (start > end || start < 0 || end > array.length) { 29 | return true; 30 | } 31 | int middle = array[end]; 32 | int leftEnd = start; 33 | for (; leftEnd < end; leftEnd++) { 34 | if (array[leftEnd] >= middle) { 35 | break; 36 | } 37 | } 38 | leftEnd -= 1; 39 | for (int i = leftEnd + 1; i < end; i++) { 40 | if (array[i] < middle) { 41 | return false; 42 | } 43 | } 44 | return isBinarySearchTree(array, start, leftEnd) && isBinarySearchTree(array, leftEnd + 1, end - 1); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/leetcode/dp/WordBreak.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.leetcode.dp; 2 | 3 | /** 4 | * test for dynamic programming 5 | *

6 | * // Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, 7 | * determine if s can be segmented into a space-separated sequence of one or more dictionary words. 8 | * You may assume the dictionary does not contain duplicate words. 9 | *

10 | * // For example, given 11 | * // s = "leetcode", 12 | * // dict = ["leet", "code"]. 13 | *

14 | * // Return true because "leetcode" can be segmented as "leet code". 15 | * Created by wcong on 2017/3/27. 16 | */ 17 | public class WordBreak { 18 | 19 | public static void main(String[] args) { 20 | 21 | } 22 | 23 | // suppose it has none blank char 24 | public static boolean wordBreak(String s, String[] dict) { 25 | if (dict == null || dict.length == 0 || s == null || s.length() == 0) { 26 | return false; 27 | } 28 | boolean[] result = new boolean[s.length()]; 29 | result[0] = true; 30 | for (int i = 0; i < s.length(); i++) { 31 | for (int j = i; j >= 0; j--) { 32 | if (isInDict(s.substring(j, i + 1), dict) && result[j]) { 33 | result[i + 1] = true; 34 | break; 35 | } 36 | } 37 | } 38 | return result[s.length()]; 39 | } 40 | 41 | 42 | private static boolean isInDict(String s, String[] dict) { 43 | for (String word : dict) { 44 | if (word.equals(s)) { 45 | return true; 46 | } 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/CacheTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring; 2 | 3 | import org.springframework.cache.CacheManager; 4 | import org.springframework.cache.annotation.Cacheable; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.cache.concurrent.ConcurrentMapCacheManager; 7 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * @author wcong 14 | * @since 16/2/22 15 | */ 16 | @Configuration 17 | @EnableCaching 18 | public class CacheTest { 19 | 20 | public static void main(String[] args) { 21 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 22 | annotationConfigApplicationContext.register(CacheTest.class); 23 | annotationConfigApplicationContext.refresh(); 24 | ComponentTest componentTest = annotationConfigApplicationContext.getBean(ComponentTest.class); 25 | System.out.println("get " + componentTest.getHelloWorld()); 26 | System.out.println("get " + componentTest.getHelloWorld()); 27 | System.out.println("get " + componentTest.getHelloWorld()); 28 | } 29 | 30 | @Bean(name = "defaultCache") 31 | public CacheManager getCacheManeger() { 32 | return new ConcurrentMapCacheManager(); 33 | } 34 | 35 | @Component 36 | public static class ComponentTest { 37 | @Cacheable("defaultCache") 38 | public String getHelloWorld() { 39 | String helloWorld = "hello world"; 40 | System.out.println(helloWorld); 41 | return helloWorld; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/stack/StackPushPopRule.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.stack; 2 | 3 | import org.springframework.util.Assert; 4 | 5 | /** 6 | * test for stack operation 7 | * give two array of integer,first is the order of push,judge second is the one of the order of pop 8 | * for example push array 1,2,3,4,5 and pop order 4,5,3,2,1 is correct but 4,3,5,1,2 is not 9 | * 10 | * @author wcong 11 | * @since 2017/3/26 12 | */ 13 | public class StackPushPopRule { 14 | 15 | public static void main(String[] args) { 16 | Assert.isTrue(isPopToPush(new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3, 2, 1})); 17 | Assert.isTrue(!isPopToPush(new int[]{1, 2, 3, 4, 5}, new int[]{4, 3, 5, 1, 2})); 18 | } 19 | 20 | public static boolean isPopToPush(int[] pushArray, int[] popArray) { 21 | if (pushArray == null || popArray == null) { 22 | return false; 23 | } 24 | int index = -1; 25 | for (int push : pushArray) { 26 | index += 1; 27 | if (push == popArray[0]) { 28 | break; 29 | } 30 | } 31 | if (index < 0) { 32 | return false; 33 | } 34 | int left = index - 1, right = index + 1; 35 | for (int i = 1; i < popArray.length; i++) { 36 | if (left >= 0 && pushArray[left] == popArray[i]) { 37 | left -= 1; 38 | continue; 39 | } 40 | if (right < pushArray.length && pushArray[right] == popArray[i]) { 41 | right += 1; 42 | continue; 43 | } 44 | return false; 45 | } 46 | return true; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/aop/CustomizeAspectTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.spring.aop; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author wcong 10 | * @since 16/4/12 11 | */ 12 | @Configuration 13 | public class CustomizeAspectTest { 14 | 15 | public static void main(String[] args) { 16 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); 17 | annotationConfigApplicationContext.register(CustomizeAspectTest.class); 18 | annotationConfigApplicationContext.refresh(); 19 | Test test = annotationConfigApplicationContext.getBean(Test.class); 20 | test.test(); 21 | } 22 | 23 | @Component 24 | public static class Test { 25 | public void test() { 26 | System.out.println("hello world"); 27 | } 28 | } 29 | 30 | @MyAspect(pointCut = "org.wcong.test.spring.aop.CustomizeAspectTest.Test.test") 31 | public static class MyAspectClass { 32 | 33 | void before(Object[] args) { 34 | System.out.println("aop before"); 35 | } 36 | 37 | void after(Object[] args) { 38 | System.out.println("aop after"); 39 | } 40 | 41 | void around(MethodInvocation methodInvocation, Object[] args) throws Throwable { 42 | System.out.println("aop around before"); 43 | methodInvocation.proceed(methodInvocation); 44 | System.out.println("aop around after"); 45 | } 46 | } 47 | 48 | @Bean 49 | public CustomizeAspectProxy getCustomizeAspectScan() { 50 | return new CustomizeAspectProxy(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/dagger/DaggerTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.dagger; 2 | 3 | import dagger.Binds; 4 | import dagger.Component; 5 | import dagger.Lazy; 6 | import dagger.Module; 7 | import dagger.Provides; 8 | 9 | import javax.inject.Inject; 10 | import javax.inject.Singleton; 11 | 12 | /** 13 | * @author wcong 14 | * @since 2016/11/2 15 | */ 16 | public class DaggerTest { 17 | public static void main(String[] args) { 18 | Coffee coffee = DaggerDaggerTest_Coffee.builder().build(); 19 | coffee.maker().brew(); 20 | } 21 | 22 | @Singleton 23 | @Component(modules = { DripCoffeeModule.class }) 24 | public interface Coffee { 25 | CoffeeMaker maker(); 26 | } 27 | 28 | @Module(includes = PumpModule.class) 29 | static class DripCoffeeModule { 30 | @Provides 31 | @Singleton 32 | Heater provideHeater() { 33 | return new ElectricHeater(); 34 | } 35 | } 36 | 37 | @Module 38 | abstract class PumpModule { 39 | @Binds 40 | abstract Pump providePump(Thermosiphon pump); 41 | } 42 | 43 | static class CoffeeMaker { 44 | Lazy heater; 45 | 46 | Pump pump; 47 | 48 | @Inject 49 | CoffeeMaker(Lazy heater, Pump pump) { 50 | this.heater = heater; 51 | this.pump = pump; 52 | } 53 | 54 | public void brew() { 55 | pump.pump(); 56 | } 57 | } 58 | 59 | interface Heater { 60 | } 61 | 62 | interface Pump { 63 | void pump(); 64 | } 65 | 66 | static class Thermosiphon implements Pump { 67 | private final Heater heater; 68 | 69 | @Inject 70 | Thermosiphon(Heater heater) { 71 | this.heater = heater; 72 | } 73 | 74 | public void pump() { 75 | System.out.println(heater); 76 | } 77 | } 78 | 79 | static class ElectricHeater implements Heater { 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/BeanPostProcessorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2015, NetEase, Inc. All Rights Reserved. 3 | * Date: 16/1/15 4 | */ 5 | package org.wcong.test.spring; 6 | 7 | import org.springframework.beans.BeansException; 8 | import org.springframework.beans.factory.config.BeanPostProcessor; 9 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.stereotype.Service; 12 | 13 | /** 14 | * test of BeanPostProcessor 15 | * 16 | * @author wcong 17 | * @since 16/1/15 18 | */ 19 | @Configuration 20 | public class BeanPostProcessorTest { 21 | 22 | public static void main(String[] args) { 23 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext( 24 | BeanPostProcessorTest.class); 25 | PrintSomething printSomething = annotationConfigApplicationContext.getBean(PrintSomething.class); 26 | printSomething.print(); 27 | } 28 | 29 | public interface PrintSomething { 30 | void print(); 31 | } 32 | 33 | @Service 34 | public static class PrintHello implements PrintSomething { 35 | 36 | public void print() { 37 | System.out.println("hello"); 38 | } 39 | } 40 | 41 | @Service 42 | public static class BeanPostProcessorService implements BeanPostProcessor { 43 | 44 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 45 | System.out.println(bean); 46 | return bean; 47 | } 48 | 49 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 50 | System.out.println(bean); 51 | return bean; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/org/wcong/test/leetcode/array/MaxPointsOnALineTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.leetcode.array; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.wcong.test.algorithm.leetcode.array.MaxPointsOnALine; 6 | 7 | /** 8 | * @author wcong 9 | * @since 10/05/2017 10 | */ 11 | public class MaxPointsOnALineTest { 12 | 13 | @Test 14 | public void testMaxPoints() { 15 | MaxPointsOnALine maxPointsOnALine = new MaxPointsOnALine(); 16 | MaxPointsOnALine.Point[] points = new MaxPointsOnALine.Point[] { new MaxPointsOnALine.Point(0, 0), 17 | new MaxPointsOnALine.Point(1, 1), new MaxPointsOnALine.Point(2, 2), new MaxPointsOnALine.Point(1, 0), 18 | new MaxPointsOnALine.Point(2, 0) }; 19 | MaxPointsOnALine.Point[] points1 = new MaxPointsOnALine.Point[] { new MaxPointsOnALine.Point(0, 0), 20 | new MaxPointsOnALine.Point(1, 1), new MaxPointsOnALine.Point(0, 0) }; 21 | MaxPointsOnALine.Point[] points2 = new MaxPointsOnALine.Point[] { new MaxPointsOnALine.Point(1, 1),new MaxPointsOnALine.Point(1, 1), 22 | new MaxPointsOnALine.Point(2, 2) , new MaxPointsOnALine.Point(2, 2) }; 23 | //[-4,1],[-7,7],[-1,5],[9,-25] 24 | MaxPointsOnALine.Point[] points3 = new MaxPointsOnALine.Point[] { new MaxPointsOnALine.Point(-4, 1),new MaxPointsOnALine.Point(-7, 7), 25 | new MaxPointsOnALine.Point(-1, 5) , new MaxPointsOnALine.Point(9, -25) }; 26 | Assert.assertTrue(maxPointsOnALine.maxPointsBruteForce(points) == 3); 27 | Assert.assertTrue(maxPointsOnALine.maxPointsDp(points) == 3); 28 | Assert.assertTrue(maxPointsOnALine.maxPointsDp(points1) == 3); 29 | Assert.assertTrue(maxPointsOnALine.maxPointsDp(points2) == 4); 30 | Assert.assertTrue(maxPointsOnALine.maxPointsDp(points3) == 3); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/jzoffer/tree/IsSubTree.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.jzoffer.tree; 2 | 3 | import org.wcong.test.algorithm.jzoffer.util.Tree; 4 | import org.wcong.test.algorithm.jzoffer.util.TreeNode; 5 | 6 | /** 7 | * test for tree 8 | *

9 | * give tow tree,judge is one tree is other sub tree 10 | * 11 | * @author wcong 12 | * @since 2017/3/26 13 | */ 14 | public class IsSubTree { 15 | 16 | public static void main(String[] args) { 17 | 18 | } 19 | 20 | public static boolean isSubTree(Tree tree, Tree subTree) { 21 | if (tree == null || tree.root == null || subTree == null || subTree.root == null) { 22 | return false; 23 | } 24 | TreeNode subNode = subTree.root; 25 | return isSubTree(tree.root, subNode); 26 | } 27 | 28 | public static boolean isSubTree(TreeNode node, TreeNode example) { 29 | if (example == null || node == null) { 30 | return false; 31 | } 32 | if (node.value == example.value) { 33 | if (isContainTree(node, example)) { 34 | return true; 35 | } 36 | } 37 | boolean isSub = isSubTree(node.left, example); 38 | return isSub || isSubTree(node.right, example); 39 | } 40 | 41 | private static boolean isContainTree(TreeNode node, TreeNode example) { 42 | if (example == null) { 43 | return true; 44 | } 45 | if (node == null) { 46 | return false; 47 | } 48 | if (node.value == example.value && isContainTree(node.left, example.left) && isContainTree(node.right, example.right)) { 49 | return true; 50 | } 51 | return false; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.wcong 6 | learn-java-annotation 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | learn-java-annotation 11 | http://maven.apache.org 12 | 13 | 14 | 15 | com.squareup 16 | javapoet 17 | 1.7.0 18 | 19 | 20 | com.google.auto 21 | auto-common 22 | 0.8 23 | 24 | 25 | javax.inject 26 | javax.inject 27 | 1 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.apache.maven.plugins 35 | maven-compiler-plugin 36 | 2.3.2 37 | 38 | 1.7 39 | 1.7 40 | UTF-8 41 | -proc:none 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/spring/InjectListOrderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2015, NetEase, Inc. All Rights Reserved. 3 | * Date: 15/12/15 4 | */ 5 | package org.wcong.test.spring; 6 | 7 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * test for inject list 16 | * 17 | * @author wcong 18 | * @since 15/12/15 19 | */ 20 | @Configuration 21 | public class InjectListOrderTest { 22 | 23 | public static void main(String[] args) { 24 | AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext( 25 | InjectListOrderTest.class); 26 | String[] beanNames = annotationConfigApplicationContext.getBeanNamesForType(PrintSomething.class); 27 | List printSomethingList = new ArrayList(beanNames.length); 28 | for (String beanName : beanNames) { 29 | System.out.println(beanName); 30 | printSomethingList.add((PrintSomething) annotationConfigApplicationContext.getBean(beanName)); 31 | } 32 | for (PrintSomething printSomething : printSomethingList) { 33 | printSomething.print(); 34 | } 35 | } 36 | 37 | public interface PrintSomething { 38 | void print(); 39 | } 40 | 41 | @Service 42 | public static class PrintHello implements PrintSomething { 43 | 44 | public void print() { 45 | System.out.println("hello"); 46 | } 47 | } 48 | 49 | @Service 50 | public static class PrintWorld implements PrintSomething { 51 | 52 | public void print() { 53 | System.out.println("world"); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/springboot/CustomizeConditional.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | /** 10 | * Created by wcong on 2016/6/12. 11 | */ 12 | @Configuration 13 | @EnableAutoConfiguration 14 | public class CustomizeConditional { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication springApplication = new SpringApplication(CustomizeConditional.class); 18 | springApplication.setWebEnvironment(false); 19 | ConfigurableApplicationContext noneMessageConfigurableApplicationContext = springApplication.run("--logging.level.root=ERROR","--endpoints.enabled=false"); 20 | try { 21 | noneMessageConfigurableApplicationContext.getBean(HelloWorld.class).print(); 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | ConfigurableApplicationContext configurableApplicationContext = springApplication.run("--message=haha", "--logging.level.root=ERROR"); 26 | configurableApplicationContext.getBean(HelloWorld.class).print(); 27 | } 28 | 29 | @Configuration 30 | @ConditionalOnMyProperties(name = "message") 31 | public static class ConditionClass { 32 | @Bean 33 | public HelloWorld helloWorld() { 34 | return new HelloWorld(); 35 | } 36 | } 37 | 38 | private static class HelloWorld { 39 | public void print() { 40 | System.out.println("hello world"); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/algorithm/cracking/graph/FirstCommonAncestor.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.algorithm.cracking.graph; 2 | 3 | import org.wcong.test.algorithm.BinaryTree; 4 | 5 | /** 6 | * design a algorithm and write code to 7 | * find the first common ancestor of two nodes in a binary tree, 8 | * avoid store additional node in data structure 9 | * 1. has parent link ,it's like two linked list intersection 10 | * 2. no parent link like tree traversal 11 | * 12 | * @author wcong 13 | * @since 18/04/2017 14 | */ 15 | public class FirstCommonAncestor { 16 | 17 | public static void main(String[] args) { 18 | } 19 | 20 | static class Result { 21 | BinaryTree.Node node; 22 | 23 | boolean isAncestor; 24 | 25 | public Result(BinaryTree.Node node, boolean isAncestor) { 26 | this.node = node; 27 | this.isAncestor = isAncestor; 28 | } 29 | } 30 | 31 | public static Result firstCommonAncestor(BinaryTree.Node root, BinaryTree.Node node1, BinaryTree.Node node2) { 32 | if (root == null) { 33 | return new Result(null, false); 34 | } 35 | if (root.value == node1.value && root.value == node2.value) { 36 | return new Result(root, false); 37 | } 38 | Result left = firstCommonAncestor(root.left, node1, node2); 39 | if (left.isAncestor) { 40 | return left; 41 | } 42 | Result right = firstCommonAncestor(root.right, node1, node2); 43 | if (right.isAncestor) { 44 | return right; 45 | } 46 | if (left.node != null && right.node != null) { 47 | return new Result(root, true); 48 | } else if (root.value == node1.value || root.value == node2.value) { 49 | boolean isAncestor = left.node != null || right.node != null; 50 | return new Result(root, isAncestor); 51 | } else { 52 | return new Result(null, false); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/datastructure/LinkedList.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.datastructure; 2 | 3 | /** 4 | * search,insert,delete for a linked list 5 | * Created by wcong on 2017/4/5. 6 | */ 7 | public class LinkedList { 8 | 9 | private static class Node { 10 | T value; 11 | Node next; 12 | } 13 | 14 | private Node root; 15 | 16 | public Node find(T value) { 17 | Node node = root; 18 | while (root != null) { 19 | if (root.value.equals(value)) { 20 | return node; 21 | } 22 | node = node.next; 23 | } 24 | return null; 25 | } 26 | 27 | public void insert(T value) { 28 | if (root == null) { 29 | Node insert = new Node<>(); 30 | insert.value = value; 31 | root = insert; 32 | return; 33 | } 34 | Node node = root; 35 | while (true) { 36 | if (node.value == value) { 37 | return; 38 | } 39 | if (node.next == null) { 40 | break; 41 | } 42 | node = node.next; 43 | } 44 | Node insert = new Node<>(); 45 | insert.value = value; 46 | node.next = insert; 47 | } 48 | 49 | public void delete(T value) { 50 | if (root == null) { 51 | return; 52 | } 53 | if (root.value.equals(value)) { 54 | root = root.next; 55 | } 56 | Node parent = root; 57 | Node node = root.next; 58 | while (node != null && node.value != value) { 59 | parent = node; 60 | node = node.next; 61 | } 62 | if (node != null) { 63 | parent.next = node.next; 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/wcong/test/guice/GuiceTest.java: -------------------------------------------------------------------------------- 1 | package org.wcong.test.guice; 2 | 3 | import com.google.inject.AbstractModule; 4 | import com.google.inject.Guice; 5 | import com.google.inject.Injector; 6 | import com.google.inject.TypeLiteral; 7 | import com.google.inject.matcher.Matcher; 8 | import com.google.inject.spi.ProvisionListener; 9 | import com.google.inject.spi.TypeEncounter; 10 | import com.google.inject.spi.TypeListener; 11 | 12 | /** 13 | * @author wcong 14 | * @since 2016/10/29 15 | */ 16 | public class GuiceTest { 17 | 18 | public static void main(String[] args) { 19 | Injector injector = Guice.createInjector(new AbstractModule() { 20 | @Override 21 | protected void configure() { 22 | bind(BindClass.class).toInstance(new BindClass()); 23 | bindListener(new MyMatcher(), new MyTypeListener()); 24 | bindListener(new MyMatcher(), new MyProvisionListener()); 25 | } 26 | }); 27 | System.out.println(injector.getInstance(BindClass.class).message); 28 | } 29 | 30 | public static class BindClass { 31 | public String message = "hello world"; 32 | } 33 | 34 | public static class MyMatcher implements Matcher { 35 | 36 | public boolean matches(Object o) { 37 | return true; 38 | } 39 | 40 | public Matcher and(Matcher other) { 41 | return null; 42 | } 43 | 44 | public Matcher or(Matcher other) { 45 | return null; 46 | } 47 | } 48 | 49 | public static class MyTypeListener implements TypeListener { 50 | 51 | public void hear(TypeLiteral type, TypeEncounter encounter) { 52 | System.out.println("hehehe"); 53 | } 54 | } 55 | 56 | public static class MyProvisionListener implements ProvisionListener { 57 | 58 | public void onProvision(ProvisionInvocation provision) { 59 | System.out.println("hahaha"); 60 | } 61 | } 62 | 63 | } 64 | --------------------------------------------------------------------------------