Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Thursday, January 22, 2015

Regular Expression for Timestamp value

Given string: 2012-02-10 00:00:00
Task: Check if it matches java.sql.Timestamp format.
Solution: use the regular expression
((1\d{3})|(20\d{2}))-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(3[0-1])) (([0-1]\d)|(2[0-3])):([0-5]\d):([0-5]\d)|(^$)

Java way:
String pattern = "(((1\\d{3})|(20\\d{2}))-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\\d)|(3[0-1])) (([0-1]\\d)|(2[0-3])):([0-5]\\d):([0-5]\\d))|(^$)";

NOTE: the regular expression above allows empty values as well, remove |(^$) from the end of the expression to get rid of it.

PS: if you'd like to know more about regular expressions and play with it go to regexpal.com

Wednesday, November 12, 2014

Make a List

Just to summarize all the ways of list creation in Java.
Taken from: http://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java


JDK

1. List listA = new ArrayList<string>();
2. List listB = Arrays.asList("A", "B", "C")

Guava

1. List names = Lists.newArrayList("Mike", "John", "Lesly");
2. List chars = Lists.asList("A","B", new String [] {"C", "D"});

Immutable List

1. Collections.unmodifiableList(new ArrayList<string>(Arrays.asList("A","B")));
2. ImmutableList.builder()                                      // Guava
            .add("A")
            .add("B").build();
3. ImmutableList.of("A", "B");                                  // Guava
4. ImmutableList.copyOf(Lists.newArrayList("A", "B", "C"));     // Guava

Empty immutable List

1. Collections.emptyList();
2. Collections.EMPTY_LIST;

List of Characters

1. Lists.charactersOf("String")                                 // Guava
2. Lists.newArrayList(Splitter.fixedLength(1).split("String"))  // Guava

List of Integers

Ints.asList(1,2,3);                                             // Guava

A little java collections memo:

Wednesday, March 19, 2014

Override toString() method with ToStringBuilder

One fancy way of toString method implementation is in using of ToStringBuilder class.
With this class you don't need to specify all the objects for output and you get a flexible mechanism of building and formatting your object's string representaion.
Just a couple of strings:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.5</version>
</dependency>

import org.apache.commons.lang.builder.ToStringBuilder;

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

Got it!

See more information on official reference documntation: http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/builder/ToStringBuilder.html

Another very good article about ToStringBuilder by Lokesh Gupta (with nice samples)
http://howtodoinjava.com/2013/06/03/how-to-override-tostring-effectively-with-tostringbuilder/

Tuesday, October 30, 2012

Mercurial: change the commit message

I used to type the commit message for my mercurial operation with mistake typing incorrect modifier of my activity. If the server doesn't know this activity Id, my push will be rejected, Sad but true.

There is an article below about how to change the commit message and make developer happy.
http://knowledgestockpile.blogspot.ca/2010/12/changing-commit-message-of-revision-in.html
It helps me to get rid of my promlems. Although, author didn't mention in this article that the user name must be also specified in hgrc. Without username I was surprised with the message: abort: no username supplied (see "hg help config")

Thursday, October 11, 2012

Proxy settings for Ubuntu, Mint and others

I established the external connetion to the web for my virtual OS twice, and every time I had to set up proxy settings. That's a reminder which will be helpful to get rid of another one search in the web with a question like "ubuntu update proxy settings" or "mint configure proxy settings".