//How to split a data set in N parts? Assume N (in the code we call it ratio)=size of data set (S, in the code called size of data) diveded with max number of items in each subset (M, in the code called max) how to split the data set?
def max = 40
def data = [40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,10,40,20,30,40,50,60,70,80,55,64,12,28,88,10,40,40,20,30,40,50,60,70,80,20,21,55,64,12,28,88,10,40,20,99,22,43,32,31,55,64,12,28,88,30,40,50,60,70,80,55,64,12,28,88,10,40,20,20,21,99,22,43,32,31,55,64,12,28,88,10,20,30,40,50,60,70,80,20,21,99,22,43,32,31,55,64,12,28,88,11,20,30,40,50,60,70,80,20,21,99,22,43,32,31,55,64,12,28,88,20,30,40,50,60,70,80,78] as ArrayList
Double ratio =data.size()/max
Double parts = Math.ceil(ratio)
int upperBound = max;
int lowerBound = 0;
for (Integer j = new Integer(1); j<=parts;j++)
{
if (lowerBound < data.size() && upperBound <=data.size())
{
println "printing elements: "+lowerBound+"-"+upperBound
def k = data.subList(lowerBound,upperBound)
println "Size of elements: "+ k.size()
lowerBound = upperBound;
upperBound = (int) (max * (j+1)<=data.size() ? max*(j+1):data.size());
}
}
måndag 9 februari 2015
Split Data in N parts
Universal Date Util in Java
package se.nap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
public class UniversalDateUtil
{
private static final Map REGEX = null;
public static final String DEFAULT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZ";
private static final UniversalDateUtil instance = new UniversalDateUtil();
private UniversalDateUtil()
{
// Test Regex here; http://www.regexplanet.com/advanced/java/index.html before inserting new format
REGEX = new HashMap();
REGEX.put("^\\d{8}$", "yyyyMMdd");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}$", "dd MMM yyyy");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
REGEX.put("^\\d{12}$", "yyyyMMddHHmm");
REGEX.put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}Z$", "yyyy-MM-dd'T'HH:mm:ss'Z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}'T'\\d{1,2}:\\d{2}:\\d{2}Z$", "yyyy-MM-dd'T'HH:mm:ss'Z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}z$", "yyyy-MM-dd'T'HH:mm:ss'z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}z$", "yyyy-MM-dd'T'HH:mm:ss'z'");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}[A-Z]{0,3}$", "yyyy-MM-dd'T'HH:mm:ssz");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}T\\d{1,2}:\\d{2}:\\d{2}[-|+]\\d{4}$","yyyy-MM-dd'T'HH:mm:ssZZZ");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
REGEX.put("^\\d{14}$", "yyyyMMddHHmmss");
REGEX.put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
REGEX.put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
REGEX.put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
REGEX.put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
REGEX.put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
REGEX.put("^\\d{1,2}\\s[a-zA-Z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
}
public static String determineDateFormat(String dateString)
{
for (String regexp : REGEX.keySet())
{
if (dateString.matches(regexp))
{
return REGEX.get(regexp);
}
}
return null; // Unknown format.
}
public static Collection getFormats()
{
return REGEX.values();
}
public static Date parseString(String dateTime) throws ParseException
{
return parseString(dateTime, null);
}
public static Date parseString(String dateTime, Locale locale) throws ParseException
{
String format = determineDateFormat(dateTime);
if (format != null)
{
return getFormatter(format,locale).parse(dateTime);
}
return null;
}
public static String toString(final Date date)
{
return toString(date, DEFAULT_FORMAT, null, null);
}
public static String toString(final Date date, Locale locale)
{
return toString(date, DEFAULT_FORMAT, locale, null);
}
public static String toString(final Date date, final String format, Locale locale, final String timezone)
{
DateFormat formatter = getFormatter(format, locale);
if(timezone!=null)
{
final TimeZone tz = TimeZone.getTimeZone(timezone);
formatter.setTimeZone(tz);
}
return formatter.format(date);
}
private static DateFormat getFormatter(String format, Locale locale)
{
if(locale==null)
{
locale = Locale.getDefault();
}
return instance.new ConcurrentDateFormatAccess(format, locale).dateformat.get();
}
class ConcurrentDateFormatAccess
{
private ThreadLocal dateformat;
public ConcurrentDateFormatAccess(final String pattern, final Locale locale)
{
dateformat = new ThreadLocal()
{
@Override
public DateFormat get()
{
return super.get();
}
@Override
protected DateFormat initialValue()
{
return new SimpleDateFormat(pattern, locale);
}
@Override
public void remove()
{
super.remove();
}
@Override
public void set(DateFormat value)
{
super.set(value);
}
};
}
}
}
tisdag 14 oktober 2014
RSA Example...
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class EncryptionUtil {
/**
* String to hold name of the encryption algorithm.
*/
public static final String ALGORITHM = "RSA";
private static final int MAX_LENGTH = 16384;
/**
* String to hold the name of the private key file.
*/
public static final String PRIVATE_KEY_FILE = "C:/keys/private.key";
/**
* String to hold name of the public key file.
*/
public static final String PUBLIC_KEY_FILE = "C:/keys/public.key";
/**
* Generate key which contains a pair of private and public key using 16384
* bytes. Store the set of keys in Prvate.key and Public.key files.
*
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws FileNotFoundException
*/
public static void generateKey() {
try {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
//MAXIMUM KEY LENGTH
keyGen.initialize(MAX_LEGTH);
final KeyPair key = keyGen.generateKeyPair();
File privateKeyFile = new File(PRIVATE_KEY_FILE);
File publicKeyFile = new File(PUBLIC_KEY_FILE);
// Create files to store public and private key
if (privateKeyFile.getParentFile() != null) {
privateKeyFile.getParentFile().mkdirs();
}
privateKeyFile.createNewFile();
if (publicKeyFile.getParentFile() != null) {
publicKeyFile.getParentFile().mkdirs();
}
publicKeyFile.createNewFile();
// Saving the Public key in a file
ObjectOutputStream publicKeyOS = new ObjectOutputStream(
new FileOutputStream(publicKeyFile));
publicKeyOS.writeObject(key.getPublic());
publicKeyOS.close();
// Saving the Private key in a file
ObjectOutputStream privateKeyOS = new ObjectOutputStream(
new FileOutputStream(privateKeyFile));
privateKeyOS.writeObject(key.getPrivate());
privateKeyOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The method checks if the pair of public and private key has been generated.
*
* @return flag indicating if the pair of keys were generated.
*/
public static boolean areKeysPresent() {
File privateKey = new File(PRIVATE_KEY_FILE);
File publicKey = new File(PUBLIC_KEY_FILE);
if (privateKey.exists() && publicKey.exists()) {
return true;
}
return false;
}
/**
* Encrypt the plain text using public key.
*
* @param text
* : original plain text
* @param key
* :The public key
* @return Encrypted text
* @throws java.lang.Exception
*/
public static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipherText;
}
/**
* Decrypt text using private key.
*
* @param text
* :encrypted text
* @param key
* :The private key
* @return plain text
* @throws java.lang.Exception
*/
public static String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(dectyptedText);
}
}
try {
// Check if the pair of keys are present else generate those.
if (!EncryptionUtil.areKeysPresent()) {
// Method generates a pair of keys using the RSA algorithm and stores it
// in their respective files
EncryptionUtil.generateKey();
}
final String originalText = "In practice, we need to store the public and private keys somewhere. Typically, the private key will be placed on our server, and the public key distributed to clients. To store the key, we simply need to pull out the modulus and the public and private exponents, then write these numbers to some file (or put in whatever convenient place).";
ObjectInputStream inputStream = null;
// Encrypt the string using the public key
inputStream = new ObjectInputStream(new FileInputStream(EncryptionUtil.PUBLIC_KEY_FILE));
final PublicKey publicKey = (PublicKey) inputStream.readObject();
final byte[] cipherText = EncryptionUtil.encrypt(originalText, publicKey);
// Decrypt the cipher text using the private key.
inputStream = new ObjectInputStream(new FileInputStream(EncryptionUtil.PRIVATE_KEY_FILE));
final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
final String plainText = EncryptionUtil.decrypt(cipherText, privateKey);
// Printing the Original, Encrypted and Decrypted Text
System.out.println("Original: " + originalText);
System.out.println("Encrypted: " +cipherText.toString());
System.out.println("Decrypted: " + plainText);
} catch (Exception e) {
e.printStackTrace();
}
Etiketter:
Big Keys,
Decryption,
Encryption,
Groovy,
Java,
RSA
torsdag 18 september 2014
Hazelcast and Groovy
@Grapes(
@Grab(group='com.hazelcast', module='hazelcast-all', version='3.2.5')
)
import com.hazelcast.core.Hazelcast
import com.hazelcast.core.HazelcastInstance
import com.hazelcast.query.SqlPredicate
import java.util.Map
import java.util.Queue
//Bean to store
class Customer implements Serializable
{
static final long serialVersionUID = 423248904328809L;
def name;
//numeric values have to be declared with right type here
Integer internalId;
def birthDate;
}
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance()
Map customers = hazelcastInstance.getMap( "customers" )
customers.put(1, new Customer(internalId:10001, name:'Luis Dinkel', birthDate:'1976-01-23'))
customers.put(2, new Customer(internalId:10111, name:'Logan Askim', birthDate:'1971-11-13'))
def cust = customers.values(new SqlPredicate("internalId>10000 AND name LIKE '%Lui%'"));
println(cust.name)
hazelcastInstance.shutdown()
onsdag 17 september 2014
RestService with Springboot and Groovy
To test this you have to install GVM follow the instruction for your environment, I use Cygwin and GVM, when you have GVM up and running, install Springboot on GVM:
gvm install springboot
//Place this groovy-file in /cygwin/home/your_username
//To kick on the service open cygwin or another terminal and write:
//spring run your_file_name.groovy
//Now the service is started, open a browser and test the service, f.e: http://localhost:8080/greeting/John
@Grab("org.grails:gorm-hibernate4-spring-boot:1.0.0.RC1")
@Grab("com.h2database:h2:1.3.173")
import grails.persistence.*
import org.springframework.http.*
import org.springframework.web.bind.annotation.*
import static org.springframework.web.bind.annotation.RequestMethod.*
@RestController
class Application {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name)
{
return "greeting "+name
}
@RequestMapping("/greeting/{name}")
public String greeting2(@PathVariable("name") String name)
{
return "greeting "+name
}
@RequestMapping(value = '/person/add/{birthDate}/{firstName}/{lastName}', method = GET)
ResponseEntity addPerson(@PathVariable("birthDate") String birthDate,
@PathVariable("firstName") String firstName,
@PathVariable("lastName") String lastName)
{
Person.withTransaction
{
def p = new Person(firstName: firstName, lastName:lastName,birthDate:birthDate).save()
if(p)
{
return new ResponseEntity("OK", HttpStatus.CREATED )
}
else
{
return new ResponseEntity("BAD",HttpStatus.BAD_REQUEST)
}
}
}
@RequestMapping(value="/person/findByFirstName/{lang}/{value}", method = GET)
String findByFirstName(@PathVariable("lang") String lang, @PathVariable("value") String value) {
def salutation = getSalutation(lang)
def p = Person.findByFirstName(value)
return p ? "${salutation} ${p.firstName} ${p.lastName} !" : "Person not found"
}
private String getSalutation(String lang)
{
def salutation = 'Hello'
if(lang.equals("sv"))
{
salutation = 'Hej'
}
else if (lang.equals("it"))
{
salutation = 'Salve'
}
return salutation
}
}
@Entity
class Person {
String firstName
String lastName
String birthDate
}
Etiketter:
Groovy,
H2,
Hibernate,
In-memory DB,
Micro Framework,
REST,
Spring,
Web Service
söndag 14 september 2014
Low Cost Jaxb Validator
package nap.util.jaxb;
import java.lang.reflect.Field;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import org.apache.log4j.Logger;
/**
* minimal consideration of JAXB annotation
*/
public class JaxbValidator
{
private static final Logger LOG = Logger.getLogger(JaxbValidator.class);
private static StringBuilder errors = new StringBuilder();
private static String message;
/**
* Enforce 'required' attibute.
*
* Requires either no security manager is used or the default security
* manager is employed.
*
* @throws SchemaValidationException
* @see {@link Field#setAccessible(boolean)}.
*/
public static void validateRequired(T target) throws SchemaValidationException
{
try
{
Class targetClass = target.getClass();
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields)
{
XmlElement annotation = field.getAnnotation(XmlElement.class);
if (annotation != null && annotation.required())
{
try
{
field.setAccessible(true);
if (field.get(target) == null)
{
if (errors.length() != 0)
{
errors.append(" ");
}
message = String.format("%s: required field '%s' is null.", targetClass.getCanonicalName(), field.getName());
LOG.warn(message);
errors.append(message);
}
}
catch (IllegalArgumentException e)
{
LOG.warn(field.getName(), e);
}
catch (IllegalAccessException e)
{
LOG.warn(field.getName(), e);
}
}
if (field.getType().getCanonicalName().equals(List.class.getCanonicalName()))
{
field.setAccessible(true);
List list = (List) field.get(target);
for (Object obj : list)
{
validateRequired(obj);
}
}
}
if (errors.length() != 0)
{
throw new SchemaValidationException(message);
}
}
catch (IllegalArgumentException e)
{
throw new SchemaValidationException(message);
}
catch (IllegalAccessException iae)
{
throw new SchemaValidationException(message);
}
}
static class SchemaValidationException extends Exception
{
private static final long serialVersionUID = 1787869622725094010L;
SchemaValidationException(String msg)
{
super(msg);
}
}
}
fredag 12 september 2014
Embed Derby DB in Groovy
@Grapes([
@Grab(group='org.apache.derby', module='derby', version='10.10.1.1'),
@GrabConfig(systemClassLoader=true)
])
import groovy.sql.*
import java.sql.*
import java.sql.DriverManager
protocol = 'jdbc:derby:'
def props = new Properties()
props.put('user', 'user1')
props.put('password', 'user1')
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver())
//DriverManager.registerDriver()
def sql = Sql.newInstance("${protocol}derbyDB;create=true", props)
/* Creating table, adding few lines, updating one */
sql.execute('create table people(id int, name varchar(40), second_name varchar(40), phone varchar(30), email varchar(50))')
println("Created table 'people'")
sql.execute("insert into people values (1,'John', 'Doe', '123456','johndoe@company.com')")
sql.execute("insert into people values (2,'Bill', 'Brown', '324235','billbrown@company.com')")
sql.execute("insert into people values (3,'Jack', 'Daniels', '443323','jackdaniels@company.com')")
println('Inserted people')
sql.execute("update people set phone='443322', second_name='Daniel''s'where id=3")
println('Updated person')
/* Simple query */
def rows = sql.rows('SELECT * FROM people ORDER BY id')
rows.each {println it}
/* Dropping table 'people' */
sql.execute('drop table people')
println ("Table 'people' dropped")
try{
DriverManager.getConnection('jdbc:derby:;shutdown=true')
}
catch (SQLException se){
gotSQLExc = true
}
println('Finish!')
Prenumerera på:
Inlägg (Atom)