code stringlengths 23 201k | docstring stringlengths 17 96.2k | func_name stringlengths 0 235 | language stringclasses 1
value | repo stringlengths 8 72 | path stringlengths 11 317 | url stringlengths 57 377 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
public static String toSizeUnit(Long size) {
if (size == null) {
return "n/a";
}
if (size < 1024) {
return String.format("%4d", size);
}
if (size / 1024 < 1024) {
return String.format("%4dk", size / 1024);
}
if (size / (1024 * 1024) < 1024) {
return String.format("%4dm", size / (1024 * 1024)... | Formats a long value containing "number of bytes" to its megabyte representation. If the value is negative, "n/a"
will be returned. | toSizeUnit | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static String toTimeUnit(long millis) {
long seconds = millis / 1000;
if (seconds < 60) {
return String.format("%02ds", seconds);
}
if (seconds < 3600) {
return String.format("%02dm%02ds", seconds / 60, seconds % 60);
}
if (seconds < (24 * 3600)) {
return String.format("%02dh%02dm", second... | Formats a long value containing "number of bytes" to its megabyte representation. If the value is negative, "n/a"
will be returned. | toTimeUnit | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static String rightStr(String str, int length) {
return str.substring(Math.max(0, str.length() - length));
} | Returns a substring of the given string, representing the 'length' most-right characters | rightStr | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static String leftStr(String str, int length) {
return str.substring(0, Math.min(str.length(), length));
} | Returns a substring of the given string, representing the 'length' most-left characters | leftStr | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static double calcLoad(double deltaUptime, double deltaTime, int factor) {
if (deltaTime <= 0 || deltaUptime == 0) {
return 0.0;
}
return Math.min(99.99, deltaTime / (deltaUptime * factor));
} | calculates a "load", given on two deltas | calcLoad | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static long[] sortAndFilterThreadIdsByValue(Map map, int threadLimit) {
int max = Math.min(threadLimit, map.size());
List<Map.Entry> list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Ent... | Sorts a Map by its values, using natural ordering. | sortAndFilterThreadIdsByValue | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
@Override
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
} | Sorts a Map by its values, using natural ordering. | compare | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static long parseFromSize(String str) {
if (str == null || str.isEmpty()) {
return -1;
}
str = str.toLowerCase();
int fromScale = BYTE_SIZE;
try {
if (str.endsWith("kb")) {
str = str.substring(0, str.length() - 2).trim();
fromScale = KB_SIZE;
}
if (str.endsWith("k")) {
str = s... | Sorts a Map by its values, using natural ordering. | parseFromSize | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
public static void sleep(long mills) {
try {
Thread.sleep(mills);
} catch (InterruptedException e) {
}
} | Sorts a Map by its values, using natural ordering. | sleep | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/Utils.java | Apache-2.0 |
private static OptionParser createOptionParser() {
OptionParser parser = new OptionParser();
// commmon
parser.acceptsAll(Arrays.asList(new String[]{"help", "?", "h"}), "shows this help").forHelp();
parser.acceptsAll(Arrays.asList(new String[]{"n", "iteration"}),
"vjtop will exit after n output iterations ... | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | createOptionParser | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
public static void main(String[] args) {
try {
// 1. create option parser
OptionParser parser = createOptionParser();
OptionSet optionSet = parser.parse(args);
if (optionSet.has("help")) {
printHelper(parser);
System.exit(0);
}
// 2. create view
String pid = parsePid(parser, optionSet)... | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | main | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
private static VMDetailView.DetailMode parseDisplayMode(OptionSet optionSet) {
VMDetailView.DetailMode displayMode = VMDetailView.DetailMode.cpu;
if (optionSet.has("memory")) {
displayMode = VMDetailView.DetailMode.memory;
} else if (optionSet.has("totalmemory")) {
displayMode = VMDetailView.DetailMode.tota... | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | parseDisplayMode | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
private static String parsePid(OptionParser parser, OptionSet optionSet) {
Integer pid = null;
// to support PID as non option argument
if (optionSet.nonOptionArguments().size() > 0) {
pid = Integer.valueOf((String) optionSet.nonOptionArguments().get(0));
}
if (pid == null) {
System.out.println("PID c... | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | parsePid | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
private static void printHelper(OptionParser parser) {
try {
System.out.println("vjtop - java monitoring for the command-line");
System.out.println("Usage: vjtop.sh [options...] <PID>");
System.out.println("");
parser.printHelpOn(System.out);
} catch (IOException ignored) {
}
} | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | printHelper | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
private static void clearTerminal() {
if (System.getProperty("os.name").contains("Windows")) {
// hack
System.out.printf("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n");
} else if (System.getProperty("vjtop.altClear") != null) {
System.out.print('\f');
} else {
System.out.print(CLEAR... | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | clearTerminal | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
public void exit() {
view.exit();
mainThread.interrupt();
System.err.println(" Quit.");
} | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | exit | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
public void preventFlush() {
needMoreInput = true;
} | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | preventFlush | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
public void continueFlush() {
needMoreInput = false;
} | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | continueFlush | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
private void waitForInput() {
while (needMoreInput) {
Utils.sleep(1000);
}
} | VJTop entry point class.
- parses program arguments - selects console view - prints header - main "iteration loop"
@author paru | waitForInput | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java | Apache-2.0 |
public void printView() throws Exception {
long iterationStartTime = System.currentTimeMillis();
long preCpuTime = operatingSystemMXBean.getProcessCpuTime();
vmInfo.update();
if (!checkState()) {
return;
}
printJvmInfo();
if (mode == DetailMode.memory || mode == DetailMode.totalmemory) {
printTo... | "detail" view, printing detail metrics of a specific jvm. Also printing the top threads (based on the current CPU
usage)
@author paru | printView | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
public boolean shouldExit() {
return shouldExit;
} | Requests the disposal of this view - it should be called again. | shouldExit | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
public void exit() {
shouldExit = true;
} | Requests the disposal of this view - it should be called again. | exit | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
private void setWidth(Integer width) {
if (width == null) {
this.width = DEFAULT_WIDTH;
} else if (width < MIN_WIDTH) {
this.width = MIN_WIDTH;
} else {
this.width = width;
}
} | Requests the disposal of this view - it should be called again. | setWidth | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
private int getThreadNameWidth() {
return this.width - 48;
} | Requests the disposal of this view - it should be called again. | getThreadNameWidth | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
public static DetailMode parse(String mode){
switch (mode) {
case "1":
return cpu;
case "2":
return syscpu;
case "3":
return totalcpu;
case "4":
return totalsyscpu;
case "5":
return memory;
case "6":
return totalmemory;
default:
System.err.println(" Wron... | Requests the disposal of this view - it should be called again. | parse | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java | Apache-2.0 |
@Override
public void run() {
jmxClient.disconnect();
} | VMInfo retrieves or updates the metrics for a specific remote jvm, using
JmxClient.
@author paru | run | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
public static VMInfo createDeadVM(String pid, VMInfoState state) {
VMInfo vmInfo = new VMInfo();
vmInfo.state = state;
vmInfo.pid = pid;
return vmInfo;
} | Creates a dead VMInfo, representing a jvm in a given state which cannot
be attached or other monitoring issues occurred. | createDeadVM | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
public void update() throws Exception {
if (state == VMInfoState.ERROR_DURING_ATTACH || state == VMInfoState.DETACHED
|| state == VMInfoState.CONNECTION_REFUSED) {
return;
}
if (perfDataSupport) {
perfCounters = perfData.getAllCounters();
}
try {
jmxClient.flush();
updateIO();
updateCpu(... | Updates all jvm metrics to the most recent remote values | update | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateIO() {
Map<String, String> procIo = ProcFileData.getProcIO(pid);
long rchar = Utils.parseFromSize(procIo.get("rchar"));
long wchar = Utils.parseFromSize(procIo.get("wchar"));
long readBytes = Utils.parseFromSize(procIo.get("read_bytes"));
long writeBytes = Utils.parseFromSize(procIo.get("w... | Updates all jvm metrics to the most recent remote values | updateIO | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateCpu() throws Exception {
long uptimeMills = jmxClient.getRuntimeMXBean().getUptime();
long cpuTimeNanos = jmxClient.getOperatingSystemMXBean().getProcessCpuTime();
if (lastUpTimeMills > 0 && lastCPUTimeNanos > 0) {
deltaUptimeMills = uptimeMills - lastUpTimeMills;
deltaCpuTimeNanos = (cp... | Updates all jvm metrics to the most recent remote values | updateCpu | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateThreads() throws IOException {
if (perfDataSupport) {
threadActive = ((LongCounter) perfCounters.get("java.threads.live")).getLong();
threadDaemon = ((LongCounter) perfCounters.get("java.threads.daemon")).getLong();
threadPeak = ((LongCounter) perfCounters.get("java.threads.livePeak")).get... | Updates all jvm metrics to the most recent remote values | updateThreads | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateClassLoad() throws IOException {
classLoaded = jmxClient.getClassLoadingMXBean().getLoadedClassCount();
classUnLoaded = jmxClient.getClassLoadingMXBean().getUnloadedClassCount();
} | Updates all jvm metrics to the most recent remote values | updateClassLoad | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateMemoryPool() throws IOException {
MemoryPoolMXBean survivorMemoryPool = jmxClient.getMemoryPoolManager().getSurvivorMemoryPool();
if (survivorMemoryPool != null) {
surUsedBytes = survivorMemoryPool.getUsage().getUsed();
surMaxBytes = getMemoryPoolMaxOrCommited(survivorMemoryPool);
}
ed... | Updates all jvm metrics to the most recent remote values | updateMemoryPool | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateGC() throws IOException {
long youngGcCount = 0;
long youngGcTimeMills = 0;
long fullGcCount = 0;
long fullGcTimeMills = 0;
if (perfDataSupport) {
youngGcCount = ((LongCounter) perfCounters.get("sun.gc.collector.0.invocations")).getLong();
youngGcTimeMills = ((TickCounter) perfCounte... | Updates all jvm metrics to the most recent remote values | updateGC | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private void updateSafepoint() {
if (!perfDataSupport) {
return;
}
long safepointCount = ((LongCounter) perfCounters.get("sun.rt.safepoints")).getLong();
long safepointTimeMills = ((TickCounter) perfCounters.get("sun.rt.safepointTime")).getMills();
long safepointSyncTimeMills = ((TickCounter) perfCounters... | Updates all jvm metrics to the most recent remote values | updateSafepoint | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
public ThreadMXBean getThreadMXBean() throws IOException {
return jmxClient.getThreadMXBean();
} | Updates all jvm metrics to the most recent remote values | getThreadMXBean | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private long getMemoryPoolMaxOrCommited(MemoryPoolMXBean memoryPool) {
MemoryUsage usage = memoryPool.getUsage();
long max = usage.getMax();
max = max < 0 ? usage.getCommitted() : max;
return max;
} | Updates all jvm metrics to the most recent remote values | getMemoryPoolMaxOrCommited | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
private int getJavaMajorVersion() {
if (jvmVersion.startsWith("1.8")) {
return 8;
} else if (jvmVersion.startsWith("1.7")) {
return 7;
} else if (jvmVersion.startsWith("1.6")) {
return 6;
} else {
return 0;
}
} | Updates all jvm metrics to the most recent remote values | getJavaMajorVersion | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java | Apache-2.0 |
public static PerfData connect(long pid) {
try {
return new PerfIntr((int) pid);
} catch (ThreadDeath e) {
throw e;
} catch (OutOfMemoryError e) {
throw e;
} catch (Error e) {
throw new RuntimeException("Cannot perf data for process " + pid
+ " - " + e.toString());
} catch (Exception e) {
... | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | connect | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public int getMajorVersion() {
return instr.getMajorVersion();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getMajorVersion | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public int getMinorVersion() {
return instr.getMinorVersion();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getMinorVersion | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public long getModificationTimeStamp() {
return instr.getModificationTimeStamp();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getModificationTimeStamp | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public Map<String, Counter<?>> getAllCounters() {
Map<String, Counter<?>> result = new LinkedHashMap<String, PerfData.Counter<?>>();
for (Object c : instr.getAllCounters()) {
Counter<?> cc = convert(c);
result.put(cc.getName(), cc);
}
return result;
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getAllCounters | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public List<Counter<?>> findByPattern(String pattern) {
return convert(instr.findByPattern(pattern));
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | findByPattern | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@SuppressWarnings("rawtypes")
private List<Counter<?>> convert(List list) {
List<Counter<?>> cl = new ArrayList<Counter<?>>(list.size());
for (Object c : list) {
cl.add(convert(c));
}
return cl;
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | convert | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@SuppressWarnings("rawtypes")
private Counter<?> convert(Object c) {
if (c instanceof sun.management.counter.LongCounter) {
sun.management.counter.LongCounter lc = (sun.management.counter.LongCounter) c;
if (U_TICKS.equals(lc.getUnits())) {
return new TickWrapper(tick, lc);
} else {
return ne... | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | convert | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public String getName() {
return counter.getName();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getName | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public Units getUnits() {
Units u = UNIT_MAP.get(counter.getUnits());
return u == null ? Units.INVALID : u;
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getUnits | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public Variability getVariability() {
Variability v = VARIABILITY_MAP.get(counter.getVariability());
return v == null ? Variability.INVALID : v;
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getVariability | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
@SuppressWarnings("unchecked")
public T getValue() {
return (T) counter.getValue();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getValue | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public String toString() {
return counter.toString().replace((char) 0, ' ');
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | toString | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public String getString() {
return trim(((sun.management.counter.StringCounter) counter)
.stringValue());
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getString | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
private String trim(String value) {
int n = value.indexOf(0);
if (n >= 0) {
return value.substring(0, n);
} else {
return value;
}
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | trim | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public long getLong() {
return ((sun.management.counter.LongCounter) counter)
.longValue();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getLong | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public double getTick() {
return tick;
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getTick | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public long getTicks() {
return getLong();
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getTicks | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
@Override
public long getMills() {
return (long) ((tick * getLong())/Utils.NANOS_TO_MILLS);
} | Wraps {@link PerfInstrumentation} class. Its purpose is to shield warnings
and {@link NoClassDefFoundError}s.
@author Alexey Ragozin (alexey.ragozin@gmail.com) | getMills | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/PerfData.java | Apache-2.0 |
public static SnapshotMBeanServerConnection newSnapshot(MBeanServerConnection mbsc) {
final InvocationHandler ih = new SnapshotInvocationHandler(mbsc);
return (SnapshotMBeanServerConnection) Proxy.newProxyInstance(Snapshot.class.getClassLoader(),
new Class[] { SnapshotMBeanServerConnection.class }, ih);
} | Flush all cached values of attributes. | newSnapshot | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
synchronized void flush() {
cachedValues = newMap();
} | Flush all cached values of attributes. | flush | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
if (methodName.equals("getAttribute")) {
return getAttribute((ObjectName) args[0], (String) args[1]);
} else if (methodName.equals("getAttributes")) {
return getAt... | Flush all cached values of attributes. | invoke | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
private Object getAttribute(ObjectName objName, String attrName) throws MBeanException,
InstanceNotFoundException, AttributeNotFoundException, ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(objName, Collections.singleton(attrName));
Object value = values.get(attrName);
i... | Flush all cached values of attributes. | getAttribute | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
private AttributeList getAttributes(ObjectName objName, String[] attrNames) throws InstanceNotFoundException,
ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(objName, new TreeSet<String>(Arrays.asList(attrNames)));
final AttributeList list = new AttributeList();
for (Stri... | Flush all cached values of attributes. | getAttributes | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames)
throws InstanceNotFoundException, ReflectionException, IOException {
NameValueMap values = cachedValues.get(objName);
if (values != null && values.keySet().containsAll(attrNames)) {
return values;
}
attr... | Flush all cached values of attributes. | getCachedAttributes | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
private static <K, V> Map<K, V> newMap() {
return new HashMap<K, V>();
} | Flush all cached values of attributes. | newMap | java | DarLiner/vjtools | vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | https://github.com/DarLiner/vjtools/blob/master/vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java | Apache-2.0 |
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode current1 = l1;
ListNode current2 = l2;
ListNode head = new ListNode(0);
ListNode currentHead = head;
int sum = 0;
while(current1 != null || current2 != null) {
... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | addTwoNumbers | java | kdn251/interviews | company/airbnb/AddTwoNumbers.java | https://github.com/kdn251/interviews/blob/master/company/airbnb/AddTwoNumbers.java | MIT |
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) {
return null;
}
TreeNode root = helper(nums, 0, nums.length - 1);
return root;
} | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | sortedArrayToBST | java | kdn251/interviews | company/airbnb/ConvertSortedArrayToBinarySearchTree.java | https://github.com/kdn251/interviews/blob/master/company/airbnb/ConvertSortedArrayToBinarySearchTree.java | MIT |
private TreeNode helper(int[] nums, int start, int end) {
if(start <= end) {
int mid = (start + end) / 2;
TreeNode current = new TreeNode(nums[mid]);
current.left = helper(nums, start, mid - 1);
current.right = helper(nums, mid + 1, end);... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | helper | java | kdn251/interviews | company/airbnb/ConvertSortedArrayToBinarySearchTree.java | https://github.com/kdn251/interviews/blob/master/company/airbnb/ConvertSortedArrayToBinarySearchTree.java | MIT |
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) {
return null;
}
PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
@Override
public int compare(ListNode o1,ListNode o2... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | mergeKLists | java | kdn251/interviews | company/airbnb/MergeKSortedLists.java | https://github.com/kdn251/interviews/blob/master/company/airbnb/MergeKSortedLists.java | MIT |
@Override
public int compare(ListNode o1,ListNode o2){
if (o1.val<o2.val) {
return -1;
} else if (o1.val==o2.val) {
return 0;
} else {
return 1;
}
} | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | compare | java | kdn251/interviews | company/airbnb/MergeKSortedLists.java | https://github.com/kdn251/interviews/blob/master/company/airbnb/MergeKSortedLists.java | MIT |
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode current1 = l1;
ListNode current2 = l2;
ListNode head = new ListNode(0);
ListNode currentHead = head;
int sum = 0;
while(current1 != null || current2 != null) {
sum /=... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | addTwoNumbers | java | kdn251/interviews | company/amazon/AddTwoNumbers.java | https://github.com/kdn251/interviews/blob/master/company/amazon/AddTwoNumbers.java | MIT |
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(root == null) {
return result;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
List<Intege... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | levelOrder | java | kdn251/interviews | company/amazon/BinaryTreeLevelOrderTraversal.java | https://github.com/kdn251/interviews/blob/master/company/amazon/BinaryTreeLevelOrderTraversal.java | MIT |
public boolean insert(int val) {
if(!map.containsKey(val)) {
map.put(val, val);
values.add(val);
return true;
}
else {
return false;
}
} | Inserts a value to the set. Returns true if the set did not already contain the specified element. | insert | java | kdn251/interviews | company/amazon/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/amazon/InsertDeleteGetRandomO1.java | MIT |
public boolean remove(int val) {
if(map.containsKey(val)) {
map.remove(val);
values.remove(values.indexOf(val));
return true;
}
return false;
} | Removes a value from the set. Returns true if the set contained the specified element. | remove | java | kdn251/interviews | company/amazon/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/amazon/InsertDeleteGetRandomO1.java | MIT |
public int getRandom() {
int random = (int)(Math.random() * values.size());
int valueToReturn = values.get(random);
return map.get(valueToReturn);
} | Get a random element from the set. | getRandom | java | kdn251/interviews | company/amazon/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/amazon/InsertDeleteGetRandomO1.java | MIT |
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while(fast != null && fast.next != null && fast != slow) {
slow = slow.next;
fast = fast.... | Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} | hasCycle | java | kdn251/interviews | company/amazon/LinkedListCycle.java | https://github.com/kdn251/interviews/blob/master/company/amazon/LinkedListCycle.java | MIT |
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | lowestCommonAncestor | java | kdn251/interviews | company/amazon/LowestCommonAncestorOfABinaryTree.java | https://github.com/kdn251/interviews/blob/master/company/amazon/LowestCommonAncestorOfABinaryTree.java | MIT |
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) {
return null;
}
PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
@Override
public int compare(ListNode o1,ListNode o2... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | mergeKLists | java | kdn251/interviews | company/amazon/MergeKSortedLists.java | https://github.com/kdn251/interviews/blob/master/company/amazon/MergeKSortedLists.java | MIT |
@Override
public int compare(ListNode o1,ListNode o2){
if (o1.val<o2.val) {
return -1;
} else if (o1.val==o2.val) {
return 0;
} else {
return 1;
}
} | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | compare | java | kdn251/interviews | company/amazon/MergeKSortedLists.java | https://github.com/kdn251/interviews/blob/master/company/amazon/MergeKSortedLists.java | MIT |
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
Stack<Integer> stack = new Stack<Integer>();
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | isPalindrome | java | kdn251/interviews | company/amazon/PalindromeLinkedList.java | https://github.com/kdn251/interviews/blob/master/company/amazon/PalindromeLinkedList.java | MIT |
public ListNode reverseList(ListNode head) {
if(head == null) {
return head;
}
ListNode newHead = null;
while(head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
... | Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} | reverseList | java | kdn251/interviews | company/amazon/ReverseLinkedList.java | https://github.com/kdn251/interviews/blob/master/company/amazon/ReverseLinkedList.java | MIT |
public boolean isValidBST(TreeNode root) {
if(root == null) {
return true;
}
return validBSTRecursive(root, Long.MIN_VALUE, Long.MAX_VALUE);
} | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | isValidBST | java | kdn251/interviews | company/amazon/ValidateBinarySearchTree.java | https://github.com/kdn251/interviews/blob/master/company/amazon/ValidateBinarySearchTree.java | MIT |
public boolean validBSTRecursive(TreeNode root, long minValue, long maxValue) {
if(root == null) {
return true;
} else if(root.val >= maxValue || root.val <= minValue) {
return false;
} else {
return validBSTRecursive(root.left, minValue, root.val) && validBST... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | validBSTRecursive | java | kdn251/interviews | company/amazon/ValidateBinarySearchTree.java | https://github.com/kdn251/interviews/blob/master/company/amazon/ValidateBinarySearchTree.java | MIT |
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while(fast != null && fast.next != null && fast != slow) {
slow = slow.next;
fast = fast.... | Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} | hasCycle | java | kdn251/interviews | company/bloomberg/LinkedListCycle.java | https://github.com/kdn251/interviews/blob/master/company/bloomberg/LinkedListCycle.java | MIT |
public boolean hasNext() {
return stack.isEmpty() ? false : true;
} | @return whether we have a next smallest number | hasNext | java | kdn251/interviews | company/facebook/BinarySearchTreeIterator.java | https://github.com/kdn251/interviews/blob/master/company/facebook/BinarySearchTreeIterator.java | MIT |
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(root == null) {
return result;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
Lis... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | levelOrder | java | kdn251/interviews | company/facebook/BinaryTreeLevelOrderTraversal.java | https://github.com/kdn251/interviews/blob/master/company/facebook/BinaryTreeLevelOrderTraversal.java | MIT |
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<String>();
if(root == null) {
return result;
}
helper(new String(), root, result);
return result;
} | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | binaryTreePaths | java | kdn251/interviews | company/facebook/BinaryTreePaths.java | https://github.com/kdn251/interviews/blob/master/company/facebook/BinaryTreePaths.java | MIT |
public void helper(String current, TreeNode root, List<String> result) {
if(root.left == null && root.right == null) {
result.add(current + root.val);
}
if(root.left != null) {
helper(current + root.val + "->", root.left, result);
}
if(root.right != null... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | helper | java | kdn251/interviews | company/facebook/BinaryTreePaths.java | https://github.com/kdn251/interviews/blob/master/company/facebook/BinaryTreePaths.java | MIT |
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if(root == null) {
return result;
}
Map<Integer, ArrayList<Integer>> map = new HashMap<>();
Queue<TreeNode> q = new LinkedList<>();
Queue<Intege... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | verticalOrder | java | kdn251/interviews | company/facebook/BinaryTreeVerticalOrderTraversal.java | https://github.com/kdn251/interviews/blob/master/company/facebook/BinaryTreeVerticalOrderTraversal.java | MIT |
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null) {
return null;
}
if(map.containsKey(node.label)) {
return map.get(node.label);
}
UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
... | Definition for undirected graph.
class UndirectedGraphNode {
int label;
List<UndirectedGraphNode> neighbors;
UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
}; | cloneGraph | java | kdn251/interviews | company/facebook/CloneGraph.java | https://github.com/kdn251/interviews/blob/master/company/facebook/CloneGraph.java | MIT |
@Override
public Integer next() {
return stack.pop().getInteger();
} | // This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();
// @return the single ... | next | java | kdn251/interviews | company/facebook/FlattenNestedListIterator.java | https://github.com/kdn251/interviews/blob/master/company/facebook/FlattenNestedListIterator.java | MIT |
@Override
public boolean hasNext() {
while(!stack.isEmpty()) {
NestedInteger current = stack.peek();
if(current.isInteger()) {
return true;
}
stack.pop();
for(int i = current.getList().size() - 1; i >= 0; i--) {
... | // This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();
// @return the single ... | hasNext | java | kdn251/interviews | company/facebook/FlattenNestedListIterator.java | https://github.com/kdn251/interviews/blob/master/company/facebook/FlattenNestedListIterator.java | MIT |
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode successor = null;
while(root != null) {
if(p.val < root.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | inorderSuccessor | java | kdn251/interviews | company/facebook/InorderSuccessorInBST.java | https://github.com/kdn251/interviews/blob/master/company/facebook/InorderSuccessorInBST.java | MIT |
public boolean insert(int val) {
if(!map.containsKey(val)) {
map.put(val, val);
values.add(val);
return true;
}
else {
return false;
}
} | Inserts a value to the set. Returns true if the set did not already contain the specified element. | insert | java | kdn251/interviews | company/facebook/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/facebook/InsertDeleteGetRandomO1.java | MIT |
public boolean remove(int val) {
if(map.containsKey(val)) {
map.remove(val);
values.remove(values.indexOf(val));
return true;
}
return false;
} | Removes a value from the set. Returns true if the set contained the specified element. | remove | java | kdn251/interviews | company/facebook/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/facebook/InsertDeleteGetRandomO1.java | MIT |
public int getRandom() {
int random = (int)(Math.random() * values.size());
int valueToReturn = values.get(random);
return map.get(valueToReturn);
} | Get a random element from the set. | getRandom | java | kdn251/interviews | company/facebook/InsertDeleteGetRandomO1.java | https://github.com/kdn251/interviews/blob/master/company/facebook/InsertDeleteGetRandomO1.java | MIT |
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
int i = 0;
while(i < intervals.size() && intervals.get(i).end < newInterval.start) {
i++;
}
while(i < intervals.size() && intervals.get(i).start <= newInterval.end) {
newInterval = ne... | Definition for an interval.
public class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
} | insert | java | kdn251/interviews | company/facebook/InsertInterval.java | https://github.com/kdn251/interviews/blob/master/company/facebook/InsertInterval.java | MIT |
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
... | Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} | lowestCommonAncestor | java | kdn251/interviews | company/facebook/LowestCommonAncestorOfABinaryTree.java | https://github.com/kdn251/interviews/blob/master/company/facebook/LowestCommonAncestorOfABinaryTree.java | MIT |
public boolean canAttendMeetings(Interval[] intervals) {
if(intervals == null) {
return false;
}
// Sort the intervals by start time
Arrays.sort(intervals, new Comparator<Interval>() {
public int compare(Interval a, Interval b) { return a.start - b.start; }
... | Definition for an interval.
public class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
} | canAttendMeetings | java | kdn251/interviews | company/facebook/MeetingRooms.java | https://github.com/kdn251/interviews/blob/master/company/facebook/MeetingRooms.java | MIT |
public List<Interval> merge(List<Interval> intervals) {
List<Interval> result = new ArrayList<Interval>();
if(intervals == null || intervals.size() == 0) {
return result;
}
Interval[] allIntervals = intervals.toArray(new Interval[intervals.size()]);
Arrays.so... | Definition for an interval.
public class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
} | merge | java | kdn251/interviews | company/facebook/MergeIntervals.java | https://github.com/kdn251/interviews/blob/master/company/facebook/MergeIntervals.java | MIT |
public int compare(Interval a, Interval b) {
if(a.start == b.start) {
return a.end - b.end;
}
return a.start - b.start;
} | Definition for an interval.
public class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
} | compare | java | kdn251/interviews | company/facebook/MergeIntervals.java | https://github.com/kdn251/interviews/blob/master/company/facebook/MergeIntervals.java | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.