JAVA-1749 Move modules language interop and console
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.asciiart;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class AsciiArt {
|
||||
|
||||
public AsciiArt() {
|
||||
}
|
||||
|
||||
public void drawString(String text, String artChar, Settings settings) {
|
||||
BufferedImage image = getImageIntegerMode(settings.width, settings.height);
|
||||
|
||||
Graphics2D graphics2D = getGraphics2D(image.getGraphics(), settings);
|
||||
graphics2D.drawString(text, 6, ((int) (settings.height * 0.67)));
|
||||
|
||||
for (int y = 0; y < settings.height; y++) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int x = 0; x < settings.width; x++) {
|
||||
stringBuilder.append(image.getRGB(x, y) == -16777216 ? " " : artChar);
|
||||
}
|
||||
|
||||
if (stringBuilder.toString()
|
||||
.trim()
|
||||
.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println(stringBuilder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private BufferedImage getImageIntegerMode(int width, int height) {
|
||||
return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
private Graphics2D getGraphics2D(Graphics graphics, Settings settings) {
|
||||
graphics.setFont(settings.font);
|
||||
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
|
||||
return graphics2D;
|
||||
}
|
||||
|
||||
public class Settings {
|
||||
public Font font;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
public Settings(Font font, int width, int height) {
|
||||
this.font = font;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.console;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ConsoleConsoleClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Console console = System.console();
|
||||
|
||||
if (console == null) {
|
||||
System.out.print("No console available");
|
||||
return;
|
||||
}
|
||||
|
||||
String progLanguauge = console.readLine("Enter your favourite programming language: ");
|
||||
console.printf(progLanguauge + " is very interesting!");
|
||||
|
||||
char[] pass = console.readPassword("To finish, enter password: ");
|
||||
|
||||
if ("BAELDUNG".equals(pass.toString().toUpperCase()))
|
||||
console.printf("Good! Regards!");
|
||||
else
|
||||
console.printf("Nice try. Regards.");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.console;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConsoleScannerClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Please enter your name and surname: ");
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
String nameSurname = scanner.nextLine();
|
||||
|
||||
System.out.println("Please enter your gender: ");
|
||||
|
||||
char gender = scanner.next().charAt(0);
|
||||
|
||||
System.out.println("Please enter your age: ");
|
||||
|
||||
int age = scanner.nextInt();
|
||||
|
||||
System.out.println("Please enter your height in meters: ");
|
||||
|
||||
double height = scanner.nextDouble();
|
||||
|
||||
System.out.println(nameSurname + ", " + age + ", is a great " + (gender == 'm' ? "guy" : "girl") + " with " + height + " meters height" + " and " + (gender == 'm' ? "he" : "she") + " reads Baeldung.");
|
||||
|
||||
System.out.print("Have a good");
|
||||
System.out.print(" one!");
|
||||
|
||||
System.out.println("\nPlease enter number of years of experience as a developer: ");
|
||||
|
||||
BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
int i = 0;
|
||||
|
||||
try {
|
||||
i = Integer.parseInt(buffReader.readLine());
|
||||
} catch (NumberFormatException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("You are a " + (i > 5 ? "great" : "good") + " developer!");
|
||||
|
||||
int sum = 0, count = 0;
|
||||
|
||||
System.out.println("Please enter your college degrees. To finish, enter baeldung website url");
|
||||
|
||||
while (scanner.hasNextInt()) {
|
||||
int nmbr = scanner.nextInt();
|
||||
sum += nmbr;
|
||||
count++;
|
||||
}
|
||||
int mean = sum / count;
|
||||
|
||||
System.out.println("Your average degree is " + mean);
|
||||
|
||||
if (scanner.hasNext(Pattern.compile("www.baeldung.com")))
|
||||
System.out.println("Correct!");
|
||||
else
|
||||
System.out.println("Baeldung website url is www.baeldung.com");
|
||||
|
||||
if (scanner != null)
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.baeldung.printf;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PrintfExamples {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
printfNewLine();
|
||||
printfChar();
|
||||
printfString();
|
||||
printfNumber();
|
||||
printfDateTime();
|
||||
printfBoolean();
|
||||
}
|
||||
|
||||
private static void printfNewLine() {
|
||||
System.out.printf("baeldung%nline%nterminator");
|
||||
}
|
||||
|
||||
private static void printfString() {
|
||||
System.out.printf("'%s' %n", "baeldung");
|
||||
System.out.printf("'%S' %n", "baeldung");
|
||||
System.out.printf("'%15s' %n", "baeldung");
|
||||
System.out.printf("'%-10s' %n", "baeldung");
|
||||
}
|
||||
|
||||
private static void printfChar() {
|
||||
System.out.printf("%c%n", 's');
|
||||
System.out.printf("%C%n", 's');
|
||||
}
|
||||
|
||||
private static void printfNumber() {
|
||||
System.out.printf("simple integer: %d%n", 10000L);
|
||||
|
||||
System.out.printf(Locale.US, "%,d %n", 10000);
|
||||
System.out.printf(Locale.ITALY, "%,d %n", 10000);
|
||||
|
||||
System.out.printf("%f%n", 5.1473);
|
||||
System.out.printf("'%5.2f'%n", 5.1473);
|
||||
System.out.printf("'%5.2e'%n", 5.1473);
|
||||
}
|
||||
|
||||
private static void printfBoolean() {
|
||||
System.out.printf("%b%n", null);
|
||||
System.out.printf("%B%n", false);
|
||||
System.out.printf("%B%n", 5.3);
|
||||
System.out.printf("%b%n", "random text");
|
||||
}
|
||||
|
||||
private static void printfDateTime() {
|
||||
Date date = new Date();
|
||||
System.out.printf("%tT%n", date);
|
||||
System.out.printf("hours %tH: minutes %tM: seconds %tS%n", date, date, date);
|
||||
System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
|
||||
|
||||
System.out.printf("%1$tA %1$tB %1$tY %n", date);
|
||||
System.out.printf("%1$td.%1$tm.%1$ty %n", date);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
var first = {
|
||||
name: "Whiskey",
|
||||
age: 5
|
||||
};
|
||||
|
||||
var second = {
|
||||
volume: 100
|
||||
};
|
||||
|
||||
Object.bindProperties(first, second);
|
||||
|
||||
print(first.volume);
|
||||
|
||||
second.volume = 1000;
|
||||
print(first.volume);
|
||||
@@ -1 +0,0 @@
|
||||
print(__FILE__, __LINE__, __DIR__);
|
||||
@@ -1,19 +0,0 @@
|
||||
var math = {
|
||||
increment: function (num) {
|
||||
return ++num;
|
||||
},
|
||||
|
||||
failFunc: function () {
|
||||
try {
|
||||
throw "BOOM";
|
||||
} catch (e if typeof e === 'string') {
|
||||
print("String thrown: " + e);
|
||||
}
|
||||
catch (e) {
|
||||
print("this shouldn't happen!");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
math;
|
||||
@@ -1,11 +0,0 @@
|
||||
var demo = {
|
||||
__noSuchProperty__: function (propName) {
|
||||
print("Accessed non-existing property: " + propName);
|
||||
},
|
||||
|
||||
__noSuchMethod__: function (methodName) {
|
||||
print("Invoked non-existing method: " + methodName);
|
||||
}
|
||||
};
|
||||
|
||||
demo;
|
||||
@@ -1 +0,0 @@
|
||||
function increment(num) ++num;
|
||||
@@ -1,2 +0,0 @@
|
||||
print(" hello world".trimLeft());
|
||||
print("hello world ".trimRight());
|
||||
@@ -1,9 +0,0 @@
|
||||
function arrays(arr) {
|
||||
|
||||
var javaIntArray = Java.to(arr, "int[]");
|
||||
print(javaIntArray[0]);
|
||||
print(javaIntArray[1]);
|
||||
print(javaIntArray[2]);
|
||||
}
|
||||
|
||||
arrays([100, "1654", true]);
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.asciiart;
|
||||
|
||||
import java.awt.Font;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.asciiart.AsciiArt.Settings;
|
||||
|
||||
public class AsciiArtIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() {
|
||||
AsciiArt asciiArt = new AsciiArt();
|
||||
String text = "BAELDUNG";
|
||||
Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character
|
||||
|
||||
asciiArt.drawString(text, "*", settings);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
package com.baeldung.scripting;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NashornUnitTest {
|
||||
|
||||
private ScriptEngine engine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trim() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornUnitTest.class.getResourceAsStream("/js/trim.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void locations() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornUnitTest.class.getResourceAsStream("/js/locations.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindProperties() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornUnitTest.class.getResourceAsStream("/js/bind.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void magicMethods() throws ScriptException {
|
||||
engine.eval("var demo = load('classpath:js/no_such.js');" + "var tmp = demo.doesNotExist;" + "var none = demo.callNonExistingMethod()");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typedArrays() throws ScriptException {
|
||||
engine.eval(new InputStreamReader(NashornUnitTest.class.getResourceAsStream("/js/typed_arrays.js")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicUsage() throws ScriptException {
|
||||
Object result = engine.eval("var greeting='hello world';" + "print(greeting);" + "greeting");
|
||||
|
||||
Assert.assertEquals("hello world", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonObjectExample() throws ScriptException {
|
||||
Object obj = engine.eval("Java.asJSONCompatible({ number: 42, greet: 'hello', primes: [2,3,5,7,11,13] })");
|
||||
Map<String, Object> map = (Map<String, Object>) obj;
|
||||
|
||||
Assert.assertEquals("hello", map.get("greet"));
|
||||
Assert.assertTrue(List.class.isAssignableFrom(map.get("primes").getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tryCatchGuard() throws ScriptException {
|
||||
engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.failFunc();");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionsExamples() throws ScriptException {
|
||||
String script = "var list = [1, 2, 3, 4, 5];" + "var result = '';" + "for each (var i in list) {" + "result+=i+'-';" + "};" + "print(result);";
|
||||
engine.eval(script);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindingsExamples() throws ScriptException {
|
||||
Bindings bindings = engine.createBindings();
|
||||
bindings.put("count", 3);
|
||||
bindings.put("name", "baeldung");
|
||||
|
||||
String script = "var greeting='Hello ';" + "for(var i=count;i>0;i--) { " + "greeting+=name + ' '" + "}" + "greeting";
|
||||
|
||||
Object bindingsResult = engine.eval(script, bindings);
|
||||
Assert.assertEquals("Hello baeldung baeldung baeldung ", bindingsResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jvmBoundaryExamples() throws ScriptException, NoSuchMethodException {
|
||||
engine.eval("function composeGreeting(name) {" + "return 'Hello ' + name" + "}");
|
||||
|
||||
Invocable invocable = (Invocable) engine;
|
||||
|
||||
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
||||
Assert.assertEquals("Hello baeldung", funcResult);
|
||||
|
||||
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" + "var map = new HashMap();" + "map.put('hello', 'world');" + "map");
|
||||
|
||||
Assert.assertTrue(Map.class.isAssignableFrom(map.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadExamples() throws ScriptException {
|
||||
Object loadResult = engine.eval("load('classpath:js/script.js');" + "increment(5)");
|
||||
|
||||
Assert.assertEquals(6, ((Double) loadResult).intValue());
|
||||
|
||||
Object math = engine.eval("var math = loadWithNewGlobal('classpath:js/math_module.js');" + "math.increment(5);");
|
||||
|
||||
Assert.assertEquals(6.0, math);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user