Skip to content

Commit 1201b3a

Browse files
authored
Add files via upload
initial files
1 parent 3fd2825 commit 1201b3a

25 files changed

+1580
-0
lines changed

AllocationOverwrite.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
*
3+
*/
4+
package com.ankit.gcexamples;
5+
6+
import java.util.Random;
7+
8+
/**
9+
* After running this program do following steps
10+
* 1) run jps command to find out the process id of this program <pid>
11+
* 2) jstat -gcutil <pid> => shoes space in %
12+
* 3) jstat -gccause <pid> => cause of garbage collection
13+
* 4) jstat -gccapacity <pid> => amount of memory allocated min max and current
14+
* 5) jstat -gc <pid> => capacity and utilization of memory
15+
* 6) jstat -gc <pid> <ms> <no of rows> => shows total rows after each ms provided.
16+
*
17+
* This program uses default gc which allocates memory fast as uses pointer
18+
* use VM argument -XX:+UseG1GC to use G1 gc which allocate memory slowly as 1/3.
19+
*
20+
* in place of jstat use "jvisualvm" tool and its visualgc plugin which is default supplied by jdk.
21+
* @author ankitkumar
22+
*
23+
*/
24+
public class AllocationOverwrite {
25+
26+
/**
27+
* @param args
28+
* @throws InterruptedException
29+
*/
30+
public static void main(String[] args) throws InterruptedException {
31+
32+
int arraySize=1000000;
33+
GCMe gcmes[]=new GCMe[arraySize];
34+
35+
int count=0;
36+
Random random=new Random();
37+
while(true){
38+
39+
gcmes[random.nextInt(arraySize)]=new GCMe();
40+
if(count%arraySize==0){
41+
//2 million allocation a second
42+
//if(count%2000000==0){
43+
System.out.print(".");
44+
//GCMxBeanExample.workingMXBean();
45+
}
46+
count++;
47+
48+
//added to see easily what happening in "visualgc"
49+
//to see visualgc run "visualvm" command on cmd as its comes with jdk.
50+
Thread.sleep(1);
51+
}
52+
53+
}
54+
55+
}
56+
57+
class GCMe{
58+
long a;
59+
long aa;
60+
long aaa;
61+
long aaaa;
62+
long aaaaa;
63+
long aaaaaa;
64+
long aaaaaaa;
65+
long aaaaaaaa;
66+
long aaaaaaaaa;
67+
long aaaaaaaaaa;
68+
long aaaaaaaaaaa;
69+
long aaaaaaaaaaaa;
70+
long aaaaaaaaaaaaa;
71+
long aaaaaaaaaaaaaa;
72+
}

ArrayLeftRotation.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
*
3+
*/
4+
package org.gradle;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @author ankitkumar
10+
*
11+
*/
12+
public class ArrayLeftRotation {
13+
14+
private final static String EMPTY_STRING=" ";
15+
private final static int MAX_NO_OF_INTEGERS=100000;
16+
private final static int MAX_ELEMENT_VALUE=1000000;
17+
18+
/**
19+
* @param args
20+
*/
21+
public static void main(String[] args) {
22+
23+
ArrayLeftRotation.Arguments input=new ArrayLeftRotation.Arguments();
24+
ArrayLeftRotation mySolution=new ArrayLeftRotation();
25+
26+
mySolution.readInput(input);
27+
28+
if(!(input.inputArray.length==input.noOfRotations)){
29+
mySolution.rotate(input);
30+
}
31+
for(int element:input.inputArray){
32+
System.out.print(element+EMPTY_STRING);
33+
}
34+
35+
}
36+
37+
private void rotate(Arguments input){
38+
int length=input.inputArray.length;
39+
for(int pass=0;pass<input.noOfRotations;pass++){
40+
int element=input.inputArray[0];
41+
for(int index=0;index<length-1;index++){
42+
input.inputArray[index]=input.inputArray[index+1];
43+
}
44+
input.inputArray[length-1]=element;
45+
}
46+
}
47+
48+
private void readInput(Arguments input) {
49+
50+
Scanner inputScanner=new Scanner(System.in);
51+
try{
52+
String line=inputScanner.nextLine();
53+
String[] firstArg=line.split(EMPTY_STRING);
54+
if(firstArg.length!=2){
55+
throw new IllegalArgumentException("Invalid Input");
56+
}
57+
int inputArrayLength=Integer.parseInt(firstArg[0]);
58+
input.noOfRotations=Integer.parseInt(firstArg[1]);
59+
if(!(1<=inputArrayLength && inputArrayLength<=MAX_NO_OF_INTEGERS)){
60+
throw new IllegalArgumentException("Invalid Array Length");
61+
}
62+
if(!(1<=input.noOfRotations && input.noOfRotations<=inputArrayLength)){
63+
throw new IllegalArgumentException("Invalid No of Rotations");
64+
}
65+
66+
String[] inputArrayString=inputScanner.nextLine().split(EMPTY_STRING);
67+
68+
if(inputArrayLength!=inputArrayString.length){
69+
throw new IllegalArgumentException("Invalid input array");
70+
}
71+
72+
input.inputArray=new int[inputArrayLength];
73+
74+
for(int index=0;index<inputArrayLength;index++){
75+
int element=Integer.parseInt(inputArrayString[index]);
76+
if(!(1<=element && element<=MAX_ELEMENT_VALUE)){
77+
throw new IllegalArgumentException("Invalid input array element");
78+
}else{
79+
input.inputArray[index]=element;
80+
}
81+
82+
}
83+
84+
} catch (NumberFormatException e) {
85+
throw new IllegalArgumentException("Invalid input array");
86+
}finally{
87+
inputScanner.close();
88+
}
89+
}
90+
91+
public static class Arguments {
92+
93+
private int noOfRotations;
94+
private int[] inputArray;
95+
96+
}
97+
98+
}

ArraysDS.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
*
3+
*/
4+
package org.gradle;
5+
6+
import java.util.Arrays;
7+
import java.util.Scanner;
8+
9+
/**
10+
* @author ankitkumar
11+
*
12+
*/
13+
public class ArraysDS {
14+
15+
private final static String EMPTY_STRING=" ";
16+
17+
/**
18+
* @param args
19+
*/
20+
public static void main(String[] args) {
21+
22+
ArraysDS.Arguments input=new ArraysDS.Arguments();
23+
ArraysDS mySolution=new ArraysDS();
24+
25+
mySolution.readInput(input);
26+
27+
28+
mySolution.doReverse(input);
29+
30+
}
31+
32+
private void doReverse(Arguments input){
33+
for(int index=input.length-1;index>=0;index--){
34+
System.out.print(input.inputArray[index]+EMPTY_STRING);
35+
}
36+
37+
}
38+
39+
private void readInput(Arguments input) {
40+
41+
Scanner inputScanner=new Scanner(System.in);
42+
try{
43+
input.length=Integer.parseInt(inputScanner.nextLine());
44+
if(!(1<=input.length && input.length<=1000)){
45+
throw new IllegalArgumentException("Invalid length");
46+
}
47+
String[] inputArrayString=inputScanner.nextLine().split(EMPTY_STRING);
48+
if(input.length!=inputArrayString.length){
49+
throw new IllegalArgumentException("Invalid input array");
50+
}
51+
input.inputArray=new int[input.length];
52+
for(int index=0;index<input.length;index++){
53+
int element=Integer.parseInt(inputArrayString[index]);
54+
if(!(1<=element && element<=10000)){
55+
throw new IllegalArgumentException("Invalid input array element");
56+
}else{
57+
input.inputArray[index]=element;
58+
}
59+
60+
}
61+
62+
} catch (NumberFormatException e) {
63+
throw new IllegalArgumentException("Invalid input array");
64+
}finally{
65+
inputScanner.close();
66+
}
67+
}
68+
69+
public static class Arguments {
70+
71+
private int length;
72+
private int[] inputArray;
73+
74+
}
75+
76+
}

CombinedPropertyDataLoader.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
*
3+
*/
4+
package com.tangoe.components;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* @author ankitkumar
11+
*
12+
*/
13+
public class CombinedPropertyDataLoader implements PropertyDataLoader {
14+
15+
private JDBCPropertyDataLoader jdbcPropertyDataLoader;
16+
private FilePropertyDataLoader filePropertyDataLoader;
17+
18+
19+
public CombinedPropertyDataLoader(JDBCPropertyDataLoader jdbcPropertyDataLoader,
20+
FilePropertyDataLoader filePropertyDataLoader) {
21+
22+
this.jdbcPropertyDataLoader = jdbcPropertyDataLoader;
23+
this.filePropertyDataLoader = filePropertyDataLoader;
24+
25+
}
26+
27+
@Override
28+
public void startLoading() {
29+
jdbcPropertyDataLoader.startLoading();
30+
filePropertyDataLoader.startLoading();
31+
32+
}
33+
34+
@Override
35+
public void stopLoading() {
36+
jdbcPropertyDataLoader.stopLoading();
37+
filePropertyDataLoader.stopLoading();
38+
}
39+
40+
@Override
41+
public Map<String, String> getProps() {
42+
Map<String, String> props = new HashMap<>();
43+
//Database property takes precedence in case of clash.
44+
props.putAll(filePropertyDataLoader.getProps());
45+
props.putAll(jdbcPropertyDataLoader.getProps());
46+
return props;
47+
}
48+
49+
}

Command.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.triplepoint.code.components;
2+
3+
public abstract class Command {
4+
5+
protected String name;
6+
protected String description;
7+
8+
public Command(String name,String description) {
9+
this.name=name;
10+
this.description=description;
11+
}
12+
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
protected abstract void execute(String... input);
23+
24+
25+
@Override
26+
public String toString() {
27+
return name + " , description: " + description ;
28+
}
29+
30+
31+
32+
}

DateCommand.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.triplepoint.code.components;
2+
3+
import java.util.Date;
4+
5+
public class DateCommand extends Command {
6+
7+
public DateCommand(String name,String description){
8+
super(name,description);
9+
}
10+
11+
@Override
12+
protected void execute(String... input) {
13+
System.out.println(new Date());
14+
}
15+
16+
}

ExitCommand.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*
3+
*/
4+
package com.triplepoint.code.components;
5+
6+
/**
7+
* @author ankitkumar
8+
*
9+
*/
10+
public class ExitCommand extends Command {
11+
12+
public ExitCommand(String name,String description) {
13+
super(name,description);
14+
}
15+
16+
/* (non-Javadoc)
17+
* @see com.triplepoint.code.components.Command#execute(java.lang.String[])
18+
*/
19+
@Override
20+
protected void execute(String... input) {
21+
22+
System.exit(0);
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)