Myths of innovation

Posted in java by pedro | Wednesday, January 27th, 2010 at 2:58 pm

I’ve just finished “The myths of innovation book by Scott Berkun, it’s well written book, what I want to share with you. Author made great research about innovation, and categorize myths about innovation. In most cases this myths are used to excuse ourself, that we don’t try to make innovation.

Everyone should read this book, don’t wait, buy or borrow and read this book. If someday you have idea, just start making it. If you find excuse for this idea, check if this excuse is not one of those myths. The myths:

  1. The myth of epiphany
  2. We understand the history of innovation
  3. There is a method for innovation
  4. People love new ideas
  5. The lone inventor
  6. Good ideas are hard to find
  7. Your boss knows more about innovation than you
  8. The best idea win
  9. Problems and solutions
  10. Innovation is always good

One supplement for this is Derek Sivers post. He provides very easy equation.

Successful Business = Idea x Execution

Where idea is valued as:

AWFUL IDEA
-1
WEAK IDEA
1
SO-SO IDEA
5
GOOD IDEA
10
GREAT IDEA
15
BRILLIANT IDEA
20

and execution as:

NO EXECUTION
$1
WEAK EXECUTION
$1000
SO-SO- EXECUTION
$10,000
GOOD EXECUTION
$100,000
GREAT EXECUTION
$1,000,000
BRILLIANT EXECUTION
$10,000,000

Too summarize that I will provide one of my favorite quote:

“Try not. Do, or do not. There is no try”

I hope that this post helps somebody.

Guava – Google Collections

Posted in java by pedro | Saturday, January 9th, 2010 at 6:34 pm

Guava

Guava is Google Core Libraries for Java 1.5, it include extension libraries which provides many common pieces of code. Long long time ago apache starts with commons-* libraries, but unfortunately most of them lack java 5 support.

Currently Google Collection is included into Guava and on 30 Dec google released Google Collection 1.0-fianal version. This version is not 1.0 but it has frozen API. It’s nearly done, but it is used in google production projects now.

So, what’s inside ?

  • Immutable Collections
  • Multisets
  • Multimaps
  • New types
  • Static utilitis

Immutable Collections

JDK currently has utility wrapper in Collections which can change our map/set/list into unmodifiable, but this is just a partial solution because internally our collection is mutable. Immutable means that we can never change the object state. So there are:

com.google.common.collect.ImmutableMap
An immutable, hash-based Map
com.google.common.collect.ImmutableSortedMap
An immutable SortedMap
com.google.common.collect.ImmutableBiMap
An immutable BiMap
com.google.common.collect.ImmutableCollection
An immutable collection.
com.google.common.collect.ImmutableMultiset
An immutable hash-based multiset
com.google.common.collect.ImmutableSet
A high-performance immutable Set
com.google.common.collect.ImmutableSortedSet
An immutable SortedSet that stores its elements in a sorted array.

All this Collections don’t allow Null values and Maps don’t allow Null keys or values.

Multisets

Multiset is a type of collection which allows duplicate elements and has unordered equality (lists have ordered equality). A multiset is also called bag. Multiset is better from list in such operation as contains(), checking if two collection are equal ignoring order and providing histograms (providing info about element count). Additionally Multiset performance depends on the number of distinct elements.

There are a lot of different implementation of Multiset interface.

com.google.common.collect.ConcurrentHashMultiset
A multiset that supports concurrent modifications
com.google.common.collect.EnumMultiset
Multiset implementation backed by an EnumMap.
com.google.common.collect.ForwardingMultiset
A multiset which forwards all its method calls to another multiset. (Decorator pattern alike)
com.google.common.collect.HashMultiset
Multiset implementation backed by a HashMap.
com.google.common.collect.ImmutableMultiset
Immputable hash-based.
com.google.common.collect.LinkedHashMultiset
A Multiset implementation with predictable iteration order.
com.google.common.collect.TreeMultiset
Multiset which maintains the ordering of its elements.

Multimaps

Similar to Multiset is Multimap iterface. It provides abstraction like a Map but keys aren’t unique or we can thing about this that unique key has a list of values, so if you have somewhere in the code type such as Map> Multimap is for you. As 1.0 version we’ve got Multimap interface implementation such as:

BiMap

BiMap is the map with unique values. Than it can be inverted so bimap.inverse().inverse() == bimap. There are few implementation of BiMap interface:

com.google.common.collect.EnumBiMap
BiMap backed by two EnumMap instances
com.google.common.collect.EnumHashBiMap
BiMap backed by an EnumMap instance for keys-to-values, and a HashMap instance for values-to-keys.
com.google.common.collect.HashBiMap
BiMap backed by two HashMap instances.
com.google.common.collect.ImmutableBiMap
Immutable BiMap

Static Utility Class

In this category there are a lot of class which has only static members to extend and provide function which are useful but not available today.

Collections2
Provides filter with Predicate and transform with Function methods
Functions
Function extension eg compose
Lists
Provides such methods as new* for easier List creation and two additional methods partition(create sublists of the same size) and transform which takes Function as a parameter and apply it to every list element.
Maps
Provides such methods as new* for easier Map creation, difference method, filter* methods with Predicate, fromProperties, transformValues with Function parameter.
Multimaps
Provides methods such as new*, synchronized*, unmodifiable*
ObjectArrays
Provides contact and newArray methods
Sets
Provides methods: differece, filter, intersecton, new*, union
Suppliers
Some Supplier extensions
Predicates
Predicate extension eg. and, or, not etc
Preconditions
Static methods to be called at beginning of methods to check preconditions.
Objects
Helper for Object class has great hashCode(Object …) method for generating hash code for multiple objects.

And more …

Ordering
Comparator with added methods to support common functions such as min, max etc. So instead of iterate over list to find min value, we can use min(Iterable) method.
MapMaker
ConcurrentMap builder, providing any combination of these features: soft or weak keys, soft or weak values, timed expiration, and on-demand computation of values.
Function
A transformation from one object to another. Used in transform static utility methods for Collection API extension.
Predicate
Determines a true or false value for a given input. Used in filter static utility methods.
Supplier
Class that supply object of single type. Used in new* static utility methods

Final thoughts

So…, in my opinion this library is must have in every project and there are a chance that this library will be standard JDK7 library, there are a lot of structures and static utility methods which makes our code much cleaner and simpler. Googler made great job!

References


Springframework utils classes

Posted in java by pedro | Friday, September 25th, 2009 at 11:39 am

Today I talked with my friend about mocking classes when there are no setter for dependency. Typically when we use springframework @Autowire support, spring will inject dependency for us. Of course we can overcame this issue by Reflection API or by using groovy. Finally I thought that spring must have some utility class for that, simple search confirmed my thoughts. There is ReflectionUtils and we can simply use it. So while searching I spot many *Utils classes, maybe there is more such utility classes. I wrote a python script to figure.

Here is the script

#!/usr/bin/env python
#coding=utf-8
import os
sources = r"put_your_path"
utilsClassesDict = dict()

def processFile(fileName, className):
    import fileinput
    commentSpoted = False
    for line in fileinput.input(fileName):
        if commentSpoted:
            utilsClassesDict[className] = line
            fileinput.close()
        if "**" in line:
            commentSpoted = True

def processDirectory(fileName):
    fileList = os.listdir(fileName)
    for file in fileList:
        newFile = os.path.join(fileName, file)
        if os.path.isdir(newFile):
            processDirectory(newFile)
        elif os.path.isfile(newFile) and ("Utils" in file):
            processFile(newFile, file[:-5])                

def printDict():
    keys = utilsClassesDict.keys()
    keys.sort()
    for k in keys:
        print("%s %s" % (k, utilsClassesDict[k]))

if os.path.exists(sources):
    processDirectory(sources)
    printDict()
else:
    print("wrong source path %s" % sources)

And for spring 2.5.6.SEC01 it returns this list.

  • AopConfigUtils * Utility class for handling registration of AOP auto-proxy creators.
  • AopNamespaceUtils * Utility class for handling registration of auto-proxy creators used internally
  • AopProxyUtils * Utility methods for AOP proxy factories.
  • AopUtils * Utility methods for AOP support code.
  • AspectJAopUtils * Utility methods for dealing with AspectJ advisors.
  • AspectJProxyUtils * Utility methods for working with AspectJ proxies.
  • AutoProxyUtils * Utilities for auto-proxy aware components.
  • AutowireUtils * Utility class that contains various methods useful for
  • BeanDefinitionReaderUtils * Utility methods that are useful for bean definition reader implementations.
  • BeanFactoryUtils * Convenience methods operating on bean factories, in particular
  • BeanUtils * Static convenience methods for JavaBeans: for instantiating beans,
  • BindingResultUtils * Convenience methods for looking up BindingResults in a model Map.
  • BshScriptUtils * Utility methods for handling BeanShell-scripted objects.
  • ClassLoaderUtils * Utility class for diagnostic purposes, to analyze the
  • ClassUtils * Miscellaneous class utility methods. Mainly for internal use within the
  • CollectionUtils * Miscellaneous collection utility methods.
  • ConnectionFactoryUtils * Helper class for managing a JMS {@link javax.jms.ConnectionFactory}, in particular
  • DataAccessUtils * Miscellaneous utility methods for DAO implementations.
  • DataSourceUtils * Helper class that provides static methods for obtaining JDBC Connections from
  • DelegatingActionUtils * Common methods for letting Struts Actions work with a
  • DomUtils * Convenience methods for working with the DOM API,
  • ExpressionEvaluationUtils * Convenience methods for transparent access to JSP 2.0’s built-in
  • FacesContextUtils * Convenience methods to retrieve the root WebApplicationContext for a given
  • FileCopyUtils * Simple utility methods for file and stream copying.
  • FileSystemUtils * Utility methods for working with the file system.
  • FreeMarkerTemplateUtils * Utility class for working with FreeMarker.
  • HtmlUtils * Utility class for HTML escaping. Escapes and unescapes
  • JBossWorkManagerUtils * Utility class for obtaining the JBoss JCA WorkManager,
  • JRubyScriptUtils * Utility methods for handling JRuby-scripted objects.
  • JasperReportsUtils * Utility methods for working with JasperReports. Provides a set of convenience
  • JavaScriptUtils * Utility class for JavaScript escaping.
  • JdbcUtils * Generic utility methods for working with JDBC. Mainly for internal use
  • JmsUtils * Generic utility methods for working with JMS. Mainly for internal use
  • JmxMetadataUtils * Utility methods for converting Spring JMX metadata into their plain JMX equivalents.
  • JmxUtils * Collection of generic utility methods to support Spring JMX.
  • JstlUtils * Helper class for preparing JSTL views,
  • LangNamespaceUtils * @author Rob Harrop
  • LobCreatorUtils * Helper class for registering a transaction synchronization for closing
  • NamedParameterUtils * Helper methods for named parameter parsing.
  • NestedExceptionUtils * Helper class for implementing exception classes which are capable of
  • NumberUtils * Miscellaneous utility methods for number conversion and parsing.
  • ObjectUtils * Miscellaneous object utility methods. Mainly for internal use within the
  • PatternMatchUtils * Utility methods for simple pattern matching, in particular for
  • PersistenceManagerFactoryUtils * Helper class featuring methods for JDO PersistenceManager handling,
  • PortletApplicationContextUtils * Convenience methods for retrieving the root WebApplicationContext for a given
  • PortletRequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • PortletUtils * Miscellaneous utilities for portlet applications.
  • PropertiesLoaderUtils * Convenient utility methods for loading of java.util.Properties,
  • PropertyAccessorUtils * Utility methods for classes that perform bean property access
  • ReflectionUtils * Simple utility class for working with the reflection API and handling
  • RemoteInvocationUtils * General utilities for handling remote invocations.
  • RequestContextUtils * Utility class for easy access to request-specific state which has been
  • RequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • ResourcePatternUtils * Utility class for determining whether a given URL is a resource
  • ResourceUtils * Utility methods for resolving resource locations to files in the
  • RmiClientInterceptorUtils * Factored-out methods for performing invocations within an RMI client.
  • ScopedProxyUtils * Utility class for creating a scoped proxy.
  • ServletRequestUtils * Parameter extraction methods, for an approach distinct from data binding,
  • SessionFactoryUtils * Helper class featuring methods for TopLink Session handling,
  • SqlParameterSourceUtils * Class that provides helper methods for the use of {@link SqlParameterSource}
  • StatementCreatorUtils * Utility methods for PreparedStatementSetter/Creator and CallableStatementCreator
  • StringUtils * Miscellaneous {@link String} utility methods.
  • StylerUtils * Simple utility class to allow for convenient access to value
  • SystemPropertyUtils * Helper class for resolving placeholders in texts. Usually applied to file paths.
  • TagUtils * Utility class for tag library related code, exposing functionality
  • TransactionSynchronizationUtils * Utility methods for triggering specific {@link TransactionSynchronization}
  • TransformerUtils * Contains common behavior relating to {@link javax.xml.transform.Transformer Transformers}.
  • TxNamespaceUtils * @author Rob Harrop
  • TypeUtils * Utility to work with Java 5 generic type parameters.
  • UiApplicationContextUtils * Utility class for UI application context implementations.
  • ValidationUtils * Utility class offering convenient methods for invoking a {@link Validator}
  • VelocityEngineUtils * Utility class for working with a VelocityEngine.
  • WebApplicationContextUtils * Convenience methods for retrieving the root
  • WebUtils * Miscellaneous utilities for web applications.

This is quite nice list (74 utility classes), isn’t it?

Most of them are really internal for context building issues, but there are some ninjas which should be useful for us in daily job, In next post I will try to pick up some of them.

Scala Implicit Conversion

Posted in java by pedro | Tuesday, July 14th, 2009 at 6:09 pm

I’ve just finished Programming in Scala by Martin Odersky. So in next few post I will describe my thoughts about Scala. Shortly I recommend you to get know something about this language. I quote:

"If I were to pick a language to use today other than Java, it would be Scala" by James Gosling

"I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I’d probably have never created Groovy." by James Strachan.

Interesting, isn’t it?

Scala has a lot of futures we do not have in java, one thing I like about scala is that most of new features  are de facto libraries, this libraries simply extend language (the name scala comes from scalable language). 

In this post I will focus on implicit conversions. It is similar to java cast, which are explicit, but of course implicit conversion are more powerful feature, which is used in scala to extend existing libraries easier.

Implicit conversion is a function which takes one type as parameter and return other type. Example in java space will be String.valueOf(int i) which takes int type and return String object. So where is the fun in this :)

The fun comes from fact that this functions are applied implicitly by compiler with full type check. To do this compiler require to follow some rules:

  1. Implicit conversion must be in scope as single identifier, or be associated with the source or target type.

    /* put conversion into scope */
    import Preamble._  /* many libraries import in this way library implicit conversions"
    /* or  as companion object */
    class Person { ... }
    object Person {
    /* implicit conversions goes here for Person type */
    }
    
  2. One conversion at the time, so there isn’t implicit conversion chaining.
  3. There may be only one way to apply implicit conversion, otherwise compiler rise error.
  4. And the last is of course that explicit has priority.

Writing implicit conversion is very easy, we just put implicit keyword before method, and we are done. The name of the implicit conversion depends on developer, I mostly saw type2target convention as example some implicit conversion from Predef: any2ArrowAssoc, int2double etc. Of course there are also typeToTarget and typeWrapper as well.

Want to assign double to int just wirte this implicit and it works.

scala>  implicit def double2int(d:Double): Int = d.toInt
scala> val i : Int = 3.5

Ok so what we can do with implicit conversions. Implicit conversion is tried in such situations:

  1. As expected type (extended cast alike).
  2. Receiver conversion.
  3. Also as implicit parameters

First rule applies in many cases, as behind the scene action. For example assume this code:

val d : Double = 3 /* 3 is Integer */
/* this code is translated by compiler into val d: Double - Predef.int2double(3) */

It works perfectly because in fact compiler is using Predef.int2double implicit conversion. We can do in such way proper object translation to use it with current libraries.

Map( "first"->"Test", "second"->"Code")

It looks like a special syntax but in fact it is just implicit conversion in action. The code is translated into:

  Map.apply(any2ArrowAssoc("first").->("Test"), any2ArrowAssoc("second").->("Code"))

I omit generics and Predef package to make this code more readable, as we see this "syntax" in fact use any2ArrowAssoc implicit conversion defined in Predef.

As all we know with great power, comes great responsibility. It may be hard to know what is going on with my code, when implicit conversions are overused. So key fact is that always we can use implicit conversion as normal method and call it explicit. Second scala compiler has -Xprint:typer option parameter which can be use to show scala code after applying implicit conversions. I was using it to show the code of Map pseudo-syntax. Example looks like this:

/* scala code Test.scala */
object Test extends Application {
  Map( "first"->"Test", "second"->"Code")
}

Console output

pedro$ scalac -Xprint:typer Test.scala
[[syntax trees at end of typer]]// Scala source: Test.scala
package  {
  final object Test extends java.lang.Object with Application with ScalaObject {
    def this(): object Test = {
      Test.super.this();
      ()
    };
    scala.this.Predef.Map.apply[java.lang.String, java.lang.String](
scala.this.Predef.any2ArrowAssoc[java.lang.String]("first").->[java.lang.String]("Test"),
scala.this.Predef.any2ArrowAssoc[java.lang.String]("second").->[java.lang.String]("Code"))
  }
}

There are also implicit parameter which also use implict to add additional parameter to the method calls. Implicit conversion is quite simple but very powerful feature, isn’t it? What do you think about it? In my opinion is quite useful.

Maven 2.2.0 Released

Posted in java by pedro | Wednesday, July 1st, 2009 at 12:59 pm

New version of maven was released. One of the benefit is that there are many bug fixes and some regression from 2.1.0 was fixed.

The main issue when upgrading is that new maven requires Java 1.5 or later! If you still have system which must be build for Java 1.4 you’ve got trouble to overcome. There is guide how you can configure your pom to use ensure that you do not use API from later version t han suggested.

This is achieved by using animal-sniffer. Animal-sniffer is project which goal is to provide tools for developers which must use many different Java versions. For example I use java 1.4, java 5 and java 6 in my daily work.

Currently there are maven plugin and command line tool. Command line help us to find which class is compiled by which java version, it may help to spot libraries or modules compiled by higher version than our project.

Most helpful is maven plugin which can be configure in such way that it rise build error if we use API from higher java version. Assume the code:

package info.pietrowski;

public class Main {
    public static void main(String[] args) {
        BigDecimal d = new BigDecimal(10); // this constructor is since 1.5
        System.out.println(d);
    }
}

Now we configure maven to target for 1.4 java platform, but JAVA_HOME points to java 5 or higher

...

  maven-compiler-plugin
  
    1.4
    1.4
  

...

Running this program with different java version drive to the output

$ jdk1.6.0_13/bin/java -classpath test.jar info.pietrowski.Main
10
$ j2sdk1.4.2_16/bin/java -classpath test.jar info.pietrowski.Main
Exception in thread "main" java.lang.NoSuchMethodError: java.math.BigDecimal.(I)V
        at info.pietrowski.Main.main(Main.java:10)

The problem is that compiler choose BigDecimal constructor based on rt.jar, and despite produce bytecode 1.4 compatible, can run it because rt.jar in java 1.4 do not have this constructor.

The rescue for this problem is animal-sniffer maven plugin which we can configure like this:

...

    org.jvnet
    animal-sniffer
      
        
          animal-sniffer
compile
            
              check
            
            
              
                org.jvnet.animal-sniffer
                java${jdk.level}
                1.0
              
            
          
        
      
        
          org.jvnet.animal-sniffer
          java${jdk.level}
          1.0
          sig
        
     
   
...

Where jdk.level is property for target java version. Also this two resource has some configuration guidance:
Signature Checker guideline and Guide to building 1.4 project.

Currently they have signatures for

  • java1.3
  • java1.4
  • java1.5
  • java1.6

And hopefully you do not have to use older java than 1.3.

After this change pom like suggested and try to build by java higher than 1.5 we get this error message:

[INFO] [animal-sniffer:check {execution: animal-sniffer}]
[INFO] Checking unresolved references to org.jvnet.animal-sniffer:java1.4:1.0
[ERROR] Undefined reference: java/math/BigDecimal.(I)V in ...\target\classes\info\pietrowski\Main.class
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Signature errors found. Verify them and put @IgnoreJRERequirement on them.

Thanks to Marcin who test with me this configuration and finally put this into our organization parent pom for all our developers.

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.