Java Annotation Example

Categories: Java June 22nd, 2014 19:44

Annotation

package com.liguoliang.lang;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})

/**
 * http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html
 * @author Guoliang
 *
 */
public @interface Note {
	String title() default "Note";
	String details();
}

Usage


package com.test.meta;

import static org.junit.Assert.*;

import org.junit.Test;

import com.liguoliang.lang.Note;

@Note(details="This test is for Annotation")
public class TestMeta {

	@Test
	public void test() {
		
		Class klass = TestMeta.class;

		assertTrue(klass.isAnnotationPresent(Note.class));
		
		Note note =  klass.getAnnotation(Note.class);
		System.out.println("Class: " + klass.getCanonicalName() + "Note: " + note.title() + ": " + note.details());
	}

}


Output:

Class: com.test.meta.TestMetaNote: Note: This test is for Annotation

Java Generic WildCard PECS: producer-extends, consumer-super.

Categories: Java June 21st, 2014 13:49

Effective Java – Item 28: Use bounded wildcards to increase API flexibility

* PECS stands for producer-extends, consumer-super

*  Do not use wildcard types as return
types. Rather than providing additional flexibility for your users, it would  force them to use wildcard types in client code.

.

	@Test
	/**
	 * PECS stands for producer-extends, consumer-super.
	 */
	public void testAccountListUtils() {
		
		List corporationAccounts = new ArrayList();
		corporationAccounts.add(new CorporationAccount(1, "Google"));
		corporationAccounts.add(new CorporationAccount(2, "Yahoo"));
		
		List allAccounts = new ArrayList<>();
		
		AccountListUtils.mergeUserList(corporationAccounts, allAccounts);
		assertEquals(corporationAccounts.size(), allAccounts.size());
	}
package com.liguoliang.lang.collection;

import java.util.List;

import com.jpm.model.IAccount;

public class AccountListUtils < T extends IAccount >{
	
	public static  void mergeUserList(List tobeMerged, List allAccounts) {
		allAccounts.addAll(tobeMerged);
	}
}

more code

	@Test
	public void wildCard() {
		List corporationAccounts = new ArrayList<>();
		corporationAccounts.add(new CorporationAccount(1, "GE"));
		corporationAccounts.add(new CorporationAccount(2, "IBM"));
		
		IAccount account = getUserById(corporationAccounts, 2);
		System.out.println(account.getAccountName());
	}
	
	/**
	 * List and List are treated as two different types.
	 * If we use List as the parameter type, and call the method by getUserId(List, 
	 * will got this compile Error:
	 * 		The method getUserById_iAccount(List, int) in the type LearnGeneric 
	 * 		is not applicable for the arguments (List, int);
	 * @param accountList
	 * @param id
	 * @return
	 */
	public IAccount getUserById(List< ? extends IAccount> accountList, int id) {
		for (IAccount iAccount : accountList) {
			if(iAccount.getAccountId().equals(id)) {
				return iAccount;
			}
		}
		return null;
	}
	
	/**
	 * Note: getUserById(List and getaUserById(List) are not reloading methods:
	 * 		Erasure of method getUserById(List, int) is the same as another method in type LearnGeneric
	 * @param accountList
	 * @param id
	 * @return
	 */
	public IAccount getUserById_withoutWildCard(List accountList, int id) {
		for (IAccount iAccount : accountList) {
			if(iAccount.getAccountId().equals(id)) {
				return iAccount;
			}
		}
		return null;
	}

Java Training Notes: Generic Class / Iterator Example

Categories: Java June 21st, 2014 11:25

ArrayList in JDK is the best example. however, this one is created by myself:

Test

Example

Excel: Use Conditional Formatting to format cells by formula

Categories: Development Notes; Tagged with: ; @ May 15th, 2014 23:31

Requirement:

As a (junior) Excel user, I want to compare two columns and change the background colour  for the cells which is not consistent.

SNAGHTML8b7b0b

 

Steps:

1. Create New Rule:

you may select the B2, and click Conditional Formatting > New Rule:

image

Configure the formula and the format.

2. Apply the the rule:

You may right click the ‘+’and then drug,  select the ‘Fill formatting Only’ when drop.

image

Or you can use Conditional Formatting > Manage Rules:

image

to change the “Applies to…” and then “Apply”

Sybase full outer join

Categories: Database; Tagged with: ; @ May 15th, 2014 22:56

Sybase doesn’t have this feature,  however we can use lift/right join + union to get it:

SELECT * FROM A LEFT OUTER JOIN B ON…

UNION
SELECT * FROM A RIGHT OUTER JOIN B ON…

UNIN will remove the duplicated records.

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.