EmailValidator.getInstance().isValid("some.mail.address@gmail.com");Make sure to add the Commons Validator jar file and it's dependencies to your project.
Software development and photography related issues
EmailValidator.getInstance().isValid("some.mail.address@gmail.com");This question should be asked more precisely: How can array of size N filled with numbers up to N (0 to N – 1) be scanned for duplicate values (or more) with one scan (O(n))?
The answer lies in the body of the question: The fact that the array is filled with numbers up to N only, implies that a helper array of size N can be used to store the number of times each number appears in the first array. The value of the number in the first array is used as an index in the helper array. For example, if the first value in the array is: 5, then the 5th element in the helper array is incremented by 1. Of course, before incrementing the value in the helper array, we check if it is larger than 0. If it is larger than 0, the value of the first array was already counted once, and this current value is a duplicate.
The code of such method is very simple, lets have a look:
public class ArrayDuplicates {public static boolean isDuplicate(int[] arr){int size = arr.length;int[] helperArr = new int[size];for (int i = 0; i < size; i++){if (helperArr[arr[i]]++ > 0){return true;}}return false;}public static void main(String[] args){System.out.println(isDuplicate(new int[] { 2, 1, 0 }));System.out.println(isDuplicate(new int[] { 2, 0, 2 }));}}
Note, that incrementing the index of the helper array (according to the value from the first array) and checking if we have already counted this number is all done the “if” sentence.
Person person = new Person("guy");List<Person> persons = new ArrayList<Person>();persons.add(person);System.out.println(persons.contains(person));
public class GetBits {public static int countBits(byte num){int count = 0;for (int i = 0; i < 8; i++){if ((num & 1) == 1) // check if right most bit is 1{count++;}num = (byte)(num >>> 1); // shit right 1 bit, including the sign bit}return count;}
public static int countBits(byte num){int count = 0;while (num > 0){if (num % 2 == 1) // check if number have modulo{count++;}num /= 2; // divide the number by 2}return count;}
=OFFSET(data!$A$1,0,0, COUNTA(data!$A:$A), COUNTA(data!$1:$1))
Now, put in your sources root directory (usually the “src”) the file: “seam.quartz.properties” file. A typical file may look like this:<async:quartz-dispatcher/><event type="org.jboss.seam.postInitialization"><action execute="#{quartzController.scheduleTimer}"/></event>
Now, to the Java code. The code is constructed from 2 Seam beans. The first bean contains the Quartz trigger class. I looks like:org.quartz.scheduler.instanceName = Sched1org.quartz.scheduler.instanceId = 1org.quartz.scheduler.rmi.export = falseorg.quartz.scheduler.rmi.proxy = falseorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 3org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Note, that when calling to “doJob” method, a cron expression is passed. This expression describes the frequency in which the job will run (event minute in the example).package com.bashan.blog.job;import org.jboss.seam.annotations.AutoCreate;import org.jboss.seam.annotations.In;import org.jboss.seam.annotations.Name;import org.jboss.seam.async.QuartzTriggerHandle;import java.io.Serializable;import java.util.Date;/*** @author Bashan*/@Name("quartzController")@AutoCreatepublic class QuartzController implements Serializable {@InScheduleProcessor processor;private QuartzTriggerHandle quartzTriggerHandleDoJob;public void scheduleTimer() {quartzTriggerHandleDoJob = processor.doJob(new Date(), "0 0/1 * * * ?");}}
The method “doJob” is a Seam asynchronous method. This method will be executed according to the data of the cron expression given in the class: QuartzController.package com.bashan.blog.job;import org.jboss.seam.ScopeType;import org.jboss.seam.annotations.AutoCreate;import org.jboss.seam.annotations.Logger;import org.jboss.seam.annotations.Name;import org.jboss.seam.annotations.Scope;import org.jboss.seam.annotations.async.Asynchronous;import org.jboss.seam.annotations.async.Expiration;import org.jboss.seam.annotations.async.IntervalCron;import org.jboss.seam.async.QuartzTriggerHandle;import org.jboss.seam.log.Log;import java.util.Date;/*** @author Bashan*/@Name("processor")@AutoCreate@Scope(ScopeType.APPLICATION)public class ScheduleProcessor {@Loggerstatic Log log;@Asynchronouspublic QuartzTriggerHandle doJob(@Expiration Date when, @IntervalCron String interval) {System.out.println("I am a Quartz job.");log.debug("Pass some information to the log");return null;}}
<f:view contentType="text/html" />Note, that you don’t have to wrap the view tag on all of your page, if you are using Facelets (the view tag is not mandatory in Facelets), so just putting the view tag is enough (like in the example).
ImageMagik is a great open source tool that allows many conversions and manipulations of images. It supports huge amount of image types and comes as a command line tool, which makes it very easy to be executed from your favorite programming language.
Resizing an image to a maximum width is an easy task using ImageMagik. It is done with a command line tool named “convert”. For example, if we would like to proportionally resize an image named “image.jpg” to the maximum width of 160 we can do:
convert "c:\image.jpg" -resize 160 "c:\image_out.jpg"
Suppose we would like to do something a bit more complex than proportionally resizing and image to a maximum width: we would like to resize an image to a maximum width, but also crop the image to maximum height. We might want to do this kind of resize, since we want to show group of thumbnails at exactly the same size no matter what is the original image proportions are. Achieving this goal with ImageMagik is an easy task, but adds quite a few complexities to the above command.
For example, if we would like to proportionally resize an image maximum size of 100 pixels and crop the image to height of 80 pixels we have to use the “convert” command this way:
convert "c:\image.jpg" -resize 160x -resize "x160<" -resize 50% -gravity center -crop 100x100+0+0 +repage "c:\image_out.jpg"
If you really want to understand what this set of command exactly do, you can the ImageMagik documentation, which is pretty good. Note that the number 160 is exactly twice than 80. So if we would like to crop an image to height of 100 we would use the number 200.
Now, let’s have a look at a simple method, that uses hibernate HQL and the Transformers class to fill “UserActivityStat” instance with data:public class UserActivityStat{private int totalPhotos;private int totalViews;public UserActivityStat() {}public int getTotalPhotos() {return totalPhotos;}public void setTotalPhotos(int totalPhotos) {this.totalPhotos = totalPhotos;}public int getTotalViews() {return totalViews;}public void setTotalViews(int totalViews) {this.totalViews = totalViews;}}
Note, that each of the 2 columns has an alias. This alias must be the name of the property on the “UserActivityStat” class. Also note for the use of the “setResultTransformer” along the “Transformers” class.public UserActivityStat getUserActivityStat(User user){return (UserActivityStat)hibernateSession.createQuery("select count(*) as totalPhotos, sum(p.views) as totalViews " +"from Photo p where p.user = :user " +"p.dateCreated <= :now").setParameter("user", user).setTimestamp("now", new Date()).setResultTransformer(Transformers.aliasToBean(UserActivityStat.class)).uniqueResult();}
Calculating the distance between 2 ips can be done easily by using the great geo service: Maxmind.
Maxmind is an affordable geo service with broad range of solutions like:
and more…
Maxmind services comes in 2 flavors:
In order to calculate the distance between 2 IPs, we will use the free version of Maxmind. The free service is called: geoLite. we will use the service named: geoLite city. The geoLite city data contains location information. The location contains latitude and longitude information for ip ranges. Latitude and longitude information is a coordinate method used in may location services such GPS.
In order to use Maxmind we need 2 things:
Calculating the distance between 2 IPs is very easy, since Maxmind did all the hard work for us. We just have to get “Location” instance for the 2 IP and use the “distance” method of the “Location” instance. The returned result is the distance between the 2 IPs in kilometers.
Let’s have a look at a sample code that calculates the distance between Google and Apple:
package com.bashan.blog.maxmind;import com.maxmind.geoip.Location;import com.maxmind.geoip.LookupService;import java.io.IOException;/*** @author Bashan*/public class MaxmindTest {public static void main(String args[]) throws Exception {LookupService lookupService = new LookupService("C:\\GeoLiteCity.dat");Location locationGoogle = lookupService.getLocation("74.125.39.147");Location locationMicrosoft = lookupService.getLocation("17.251.200.70");System.out.println("Google is located on: " + locationGoogle.city);System.out.println("Apple is located on: " + locationMicrosoft.city);System.out.print("Distance: " + locationGoogle.distance(locationMicrosoft) + " kilometers");}}
And the output:
Not too far as you can see… ;-)Google is located on: Mountain ViewApple is located on: CupertinoDistance: 13.218970226605398 kilometers