Posts

Print two dimensional array in spiral order

Source code: package com.in2soft.test; public class PrintSpiralTest { /** * @since Dec 26, 2015 * @author Shashwat.Anand */ public static void main(String[] args) { int[][] twoDArry = { { 2, 4, 6, 8 }, { 5, 9, 12, 16 }, { 2, 11, 5, 9 }, { 3, 2, 1, 8 } }; printInSpiralOrder(twoDArry, twoDArry[0].length, twoDArry.length); } private static void printInSpiralOrder(int[][] twoDArry, int m, int n) { int t = 0, b = m - 1, l = 0, r = n - 1; int dir = 0; // 0 = -> ; 0 = bottom; 0 = <-; 0 = top while (t <= b && l <= r) { if (dir == 0) { for (int i = l; i <= r; i++) { System.out.println(twoDArry[t][i]); } t++; dir = 1; } else if (dir == 1) { for (int i = t; i <= b; i++) { System.out.println(twoDArry[i][r]); } r--; dir = 2; } else if (dir == 2) { for (int i = r; i >= l; i--) { System.out.println(twoDArry...

Find two strings are anagram in Java ?

Source Code : import java.util.Arrays; /** * Anagram.java * @author Shashwat Anand */ public class Anagram { public static void main(String[] args) { System.out.println(testAnagram("Shashwat", "Shawatsh")); System.out.println(testAnagram("Shashwat", "Shawatwh")); System.out.println(testAnagramWithInbuiltMethods("Shashwat", "Shawatsh")); System.out.println(testAnagramWithInbuiltMethods("Shashwat", "Shawatwh")); } private static boolean testAnagramWithInbuiltMethods(String str1, String str2) { char[] charArray1 = str1.toCharArray(); char[] charArray2 = str2.toCharArray(); Arrays.sort(charArray1); Arrays.sort(charArray2); return Arrays.equals(charArray1, charArray2); } private static boolean testAnagram(String str1, String str2) { ...

How to add Xtext DSL editor in Multi-Page Editor

To add your Xtext Dsl Editor to Multi-Page Editor Getting the instance of XText editor MyDslActivator activator = MyDslActivator.getInstance(); final Injector injector = activator.getInjector("org.shashwat.xtext.mydsl.MyDsl"); XtextEditor xtextEditor = injector.getInstance(XtextEditor.class); Remember to have editor input instance of IFileEditorInput. XtextEditor uses XtextDocumentProvider which extends FileDocumentProvider FileEditorInput fileEditorInput = new FileEditorInput(file); // Here file is instance of IFile int index = addPage(xtextEditor, fileEditorInput);

Run Javascript Code inside Java Code

As JDK 8 has Nashorn Javascipt Engine, below is simple code snippet for this: package com.sha.java8.jjs_nashorn; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class FirstEg { public static void main(String[] args) { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine engine = scriptEngineManager.getEngineByName("nashorn"); String script = "var greet = 'Hello'; " + "greet += ' Shashwat'; " + "greet;"; Object output; try { output = engine.eval(script); System.out.println(output); } catch (Exception e) { System.out.println("Javascript Exception"); e.printStackTrace(); } } } Output: Hello Shashwat

When to use Import-Package and Require-Bundle in Eclipse Plugin?

The OSGi way is to use Import-Package, as it specifically decouples the package from the bundle that provides it. Require-Bundle: Bundles can be re-exported Gives you access to all exports of the bundle, regardless of what they are, and regardless of whether you need them. Specifies the explicit bundle (and version) to use. If a required bundle needs to be refactored and a package moved elsewhere, then dependents will need changes to their MANIFEST.MF file. Import-Package: Loose coupling, only the package (and version) is specified and the run-time finds the required bundle. Actual implementations can be swapped out Dependent packages can be moved to different bundles by the package owner But requires more metadata to be maintained (i.e: each package name) at lower levels of granularity In my view we should below approach in using Import Package or Require-Bundle Interface <=> Import-Package Implementation class <=> ...

Creating password protected ZIP file using TrueZip

try { final TConfig config = TConfig.get(); // Request encryption in archive files. config.setOutputPreferences(config.getOutputPreferences() .or(BitField.of(FsOutputOption.ENCRYPT))); // Configure archive detector with custom key management for ZIP files. config.setArchiveDetector(newArchiveDetector1("zip", "password")); // Setup file paths. TFile src = new TFile("file1"); TFile dst = new TFile("file2"); if (dst.isArchive() || dst.isDirectory()) dst = new TFile(dst, src.getName()); // Recursive copy. src.cp_rp(dst); } finally { // Commit changes. TVFS.umount(); }

AWS vs OpenStack

AWS (Amazon Webservices)                                            OpenStack AWS Console                                                             Horizon (it is a dashboad of services) IAM                                                               ...