Featured FREE Whitepapers

What's New Here?

software-development-2-logo

Quotes relating to System Design

There are a few quotes I think of when thinking about computer design. These are not specifically about computers, but I think they are appropriate. Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away. — Antoine de Saint-Exupery, French writer (1900 – 1944) “Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it’s worth it in the end because once you get there, you can move mountains.” Steve Jobs -[BusinessWeek, May 25, 1998,] Often contentious topics can labour under the assumption that it makes any difference in the first place. DEVENTER (n) A decision that’s very hard to make because so little depends on it, such as which way to walk around a park — The Deeper Meaning of Liff by Douglas Adams and John Lloyd. Everyone can learn something from your mistakes Mistakes – It could be that the purpose of your life is only to serve as a warning to others. — Ashleigh Brilliant on on Despair.com Remember to keep what you are doing relevant Computers make it easier to do a lot of things, but most of the things they make it easier to do don’t need to be done. ~Andy Rooney Information technology and business are becoming inextricably interwoven. I don’t think anybody can talk meaningfully about one without the talking about the other. ~Bill Gates Making things simple and clear should be much of the design effort. Not just something which works. A computer will do what you tell it to do, but that may be much different from what you had in mind. ~Joseph Weizenbaum Doing very little, often enough, can quickly add up. The inside of a computer is as dumb as hell but it goes like mad! ~Richard Feynman   Reference: Quotes relating to System Design from our JCG partner Peter Lawrey at the Vanilla Java blog. ...
espertech-logo

The complex (event) world

This blog entry attempts to summarize the technologies in the CEP domain, touching over their prime feature(s) as well as their lackings.  It seems sometimes that the term CEP is being overused (as did happen with ‘ESB’) and the write-up below reflects our perception and version of it. ESPER (http://esper.codehaus.org/) is a popular open source component for complex event processing (CEP) available for java. It includes rich support for pattern matching and stream processing based on sliding time or length windows. Despite the fervent discussion over the term ‘CEP’ (http://www.dbms2.com/2011/08/25/renaming-cep-or-not/), ESPER seem to be a good fit for the term CEP, as it appears to be able to really identify “complex events” from a stream of simple events, thanks to ESPER’s EPL (Event Processing Language).Recently, while searching for an open source solution for real time CEP, our group stumbled across Twitter’s Storm project (https://github.com/nathanmarz/storm). It claims to be most comparable to Yahoo’s S4, while being in the same space as “Complex Event Processing” systems like Esper and Streambase. I am not sure about Streambase, but digging deeper into the Storm project made it look much different from CEP and from the ESPER solution. Ditto with S4 (http://incubator.apache.org/s4/). While S4 and Storm seem to be good at real time stream processing in a distributed mode and they appeared (as they claim) to be the “Hadoop for Real Time”, they didn’t seem to have provisions to match patterns (and thus to indicate complex events).Searching for a definition for CEP (that our study can relate to) led to the following bullets, (http://colinclark.sys-con.com/node/1985400) which include the below four as prerequisite for a system/solution to be called a CEP component/project/solution:Domain Specific Language  Continuous Query  Time or Length Windows Temporal Pattern MatchingThe lack of continuous query supporting time/length windows and temporal pattern matching seem to altogether missing in current versions of the S4 and Storm projects. Probably, it is due to their infancy and they will mature up to include such features in future. As of now, they only seem fit for pre-processing the event stream before passing it over to a CEP engine like ESPER. Their ability to do distributed processing (a la map-reduce mode) can help to speed up the pre-processing where events can be filtered off, or enriched via some lookup/computation etc. There have also been some attempts to integrate Storm with Esper (http://tomdzk.wordpress.com/2011/09/28/storm-esper/). While the processing systems like S4 and Storm lack important features of CEP, ESPER based systems have the disadvantage of being memory bound. Having too many events or having a large time windows can potentially cause ESPER to run out of memory. If ESPER is used to process real time streams, for e.g. from a social media, there will be lot of data accumulating in the ESPER memory. On a high level, the problem statement is to invent a CEP solution for big data. On a finer level, the problem statement include architecting a CEP solution for handling on-board (batched) as well as in-flight (Real-Time) data. In DarkStar’s terminology (http://www.eventprocessing-communityofpractice.org/EPS-presentations/Clark_EP.pdf), the requirement is “having matched a registered pattern in real time, discover similar patterns in the database”. Since being memory bound is a limitation, it may prove useful if some mechanism to condense the in-memory events can be arrived at. The condensed data however should still be meaningful and retain the context of the original stream.DarkStar does this using Symbolic Aggregate Approximation (http://www.cs.ucr.edu/~eamonn/SAX.htm), and they claim to address the aforementioned requirements by using SAX together with AsterData’s nCluster which is a mpp (massively parallel processing) database with an embedded analytics engine based on SQL/MapReduce (http://www.asterdata.com/resources/mapreduce.php). to be continued (as we research further) … Reference: The complex (event) world from our JCG partner Abhishek Jain at the NS.Infra blog....
git-logo

Git vs. SVN – Commandline Syntax Reference

Learning the git workflow takes a bit of brain retraining, but since I’ve been using SVN almost entirely via commandline (because Subversive sucks and locks up my Eclipse when I try to use it for anything beyond synching/updating/committing a handful of files), adopting git’s commandline syntax is reasonably similar. Consider these simple operations:Initial checkout from existing repo for a given branch git clone http://github.com/sonatype/sonatype-tycho.git; cd sonatype-tycho; git checkout origin/tycho-0.10.x svn checkout http://anonsvn.jboss.org/repos/jbosstools/branches/jbosstools-3.2.0.Beta1/Update locally checked out files from central repo git pull svn updateList locally changes files/folders git status svn statDiff locally changed file git diff somefile.txt svn diff somefile.txtRevert locally changed file* git checkout somefile.txt svn revert somefile.txtRevert ALL local changes (except untracked files)* git reset –hard HEAD svn revert . -RAdd new file git add file.txt svn add file.txtAdd new folder recursively git add folder svn add folderDelete file git rm file.txt svn rm file.txtDelete folder git rm -r folder (non-recursive by default; use -r to recurse) svn rm folder (recursive by default; use -N to not recurse)Commit changed file to central repo git commit -m “message” file.txt; git push svn ci -m “message” file.txtIgnore files/folders (in the current folder) echo “target *.class bin” > .gitignore; \ git ci -m “gitignore” .gitignore svn propset svn:ignore “target *.class bin” .; \ svn ci -N -m “svn:ignore” .Obviously you can do a lot more w/ Git than with SVN (like stashing local changes temporarily), but for the sake of simply moving from a VCS to a DVCS and being able to continue to work the same way you already do, the above table should provide a good introduction. Reference: Git vs. SVN – Basic Commandline Syntax Reference from our JCG partner Nick Boldt at the DivByZero blog....
apache-camel-logo

Camel: Now with Twitter and Websocket

The upcoming Apache Camel 2.10 is well underway. A lot of new components is being added, and two really exiting new components is twitter and websocket. Its yet another kick ass contributions from the community that makes this possible. James Strachan and I gave a talk last month at the JFokus conference in Stockholm. And for that conference I had prepared a little example using these new components. Unfortunately there was a problem with the projector and James laptop, so we had to give the presentation with slides only. So this weekend I found some time again, to polish the example, prepare documentation, and donate the example to the Apache Camel project. The example is demonstrating how to poll a constant feed of twitter searches and publish results in real time using web socket to a web page. As usual the code in Camel is very simple. All it takes is roughly   from(“twitter://search?…”)     .setHeader(“websocket.sendToAll”, “true”)     .to(“websocket:camel-tweet”) To use twitter, you need a twitter account which have setup an application to be used. For twitter users, you may be familiar that twitter requires you to grant applications access to your twitter account, such as twitter for iphone etc. The same applies for this example. We have described this in more details at the Camel twitter documentation. When you have created an application, you get a number of details back from twitter which you need to use the twitter component. We will get back to this later. This example is located in the examples directory of Apache Camel 2.10, in the camel-example-twitter-websocket. To run the example you use Maven with the following command: mvn compile exec:java The example will report error if you have not setup your twitter application. To do that edit the source code in the src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java class. When successfully setup the applications starts, and you can open a web browser on page http://localhost:9090/ By default we do a twitter search every 2nd second for the word “gaga”. There is usually a lot of people tweeting about Lady gaga :). You can of course change this in the aforementioned source file. Below is a screenshot of the example in action.Live feeds of gaga tweets using Camel in a web browserThe client is a web browser, so we need to setup this in a .html page using JavaScript. See the src/main/resources/index.html for the source code. The interesting piece in the source code is that its very easy to setup a websocket client   var location = “ws://localhost:9090/camel-tweet”;   this._ws=new WebSocket(location);   this._ws.onmessage=this._onmessage;   this._ws.onclose=this._onclose; Notice that all it requires is an url to pass into the WebSocket constructor. And then setup two functions for callbacks.Have fun, and I tried using Metallica as the search term, but I must admit that there are more frequent tweets about Gaga.Metallica tweetsAnyone its time to end this blog, and get the code committed. And to enjoy some Metallica music, and have a nice weekend. Reference: Camel now with twitter and websocket from our JCG partner Claus Ibsen at the Claus Ibsen riding the Apache Camel blog....
ibm-websphere-logo

WebSphere Application Server Performance Tuning Toolkit

IBM has released the WebSphere Application Server Performance Tuning Toolkit that gives the ability to monitor multiple WebSphere Application Servers from an eclipse workspace*. This tools uses WAS Performance Monitoring statistics to get and chart statistics that indicate the health and well being of your server. * Please note that the tool has been released on an as-is basis with no support. http://www.ibm.com/developerworks/websphere/downloads/peformtuning.html Why this tool instead of the other monitoring solutions 1. Its free :-) 2. No agents needed on the server 2. Monitor multiple servers3. This tool provides a standalone viewer for PMI and mbean statistics. It has a RCP GUI which uses the WebSphere application server admin thin client to call a JMX proxy, the JMX proxy will communicate with the WAS via the SOAP/RMI. 4, Predefined alerts and actions. · If the heapsize reaches the maximum heap size, it will generate a runtime alert. · If the thread pool reaches the threshold, it will generate a threadpool alert. · If the CPU usage reaches 90%, it will generate a runtime alert. · If some servlet error occur, it will generate a servlet alert. · If some connection timeout occurs, it will generate a connection alert · If there are some thread waiting for connection, it will generate a connection alert · If there are more then 1000 prepared statement discarded, it will generate a connection alert · If application try to use some invalidated session, it will generate a session alert · If there is no room for new session, it will generate a session alert · If some hung thread decleared, it will generate a threadpool alert · If some transactions rolled back, it will generate a transaction alert · If some transactions timeout, it will generate a transaction alert · You can define your own rules. 5. Monitoring The monitor panel presents performance data (Servlet Response Time, Transaction Response Time, Jdbc Response Time, Servlet Throughput, Transaction Throughput, Jdbc Throughput, Concurrency) in a classified views to help you with correlation and provide a comprehensive view of the state of the server. you analyze the performance problems in your environment.Response Time page The response time page presents the response time of a server. It collects data from four components i.e. servlet, transaction, EjbMethod and JDBC.Throughoutput page The throughoutput page presents the throughoutpage of a server. It displays data from four components. i.e. servlet, transaction, EjbMethod and JDBC.Concurrency page The concurrency page presents the concurrency of a server collecting data rom four components transaction, EjbMethod and JDBC.Alert page The alert page presents the alert evens of a server. You can find the alert detail from the alert type and alert description columns. Servlet page The servlet page presents the servlet detail informations of a server. You can find the response time, throughoutput, concurrency and error count in the columns. When you expand the row , you will get the servlet page detail of you application. Transaction page The transaction page presents the transaction information of a server. It displays response time, through output, roll back count, global timeout count, local timout count, global response time, local response time. EJB page The EJB page presents the EJB information of a server. It collects and displays response time, throughput and concurrent invocation data. When you expand the row, you can find the detail contribution of your application. The different colors mean the degree of the contributions. Connection Pool page The connection pool page presents the connection pool information of a server. It collects data from connection use time, connection through output, error count, wait time, JDBC through output, JDBC response time, JDBC concurrency, statement discard. When you expand the row, you can find the detail information of different JDBC connections. Runtime page The runtime page presents the runtime information of a server. It collects data from CPU usage, Max heap size, heap size, used heap size. Session page The session page presents the session information of a server. It collects data from live count, life time, affinity break, time out count, active non-exist count, no room count, cache discard count. When you expand the row, you can find the detail information of your different applications. The different colors mean the different contribution to the whole time. ThreadPool page The Thread pool page presents the thread pool information of a server. It collects data from max size, pool size, use pool, min size, declared thread hung count, concurrent hung thread count, cleared thread hung count. When you expand the row, you can find the detail information of different thread pools. The colors mean the different contributions. For more see http://www.ibm.com/developerworks/websphere/downloads/peformtuning.html Reference: WebSphere Application Server Performance Tuning Toolkit from our JCG partner Rohit Kelapure at the All Things WebSphere blog....
java-logo

Automaton implementation in Java

This post will address the problem of implementing finite states machines into java. If you don’t know what FSM are or where can be used you may be keen on reading this, this and this. If you ever found yourself in the situation of using a FSM on your design you have probably started to write classes for every state which implemented the same interface. A good design might have been: interface State { } class State_1 implements State {} class State_2 implements State {} ...And you probably had a class that managed these states and the transitions between them, and another that implemented the Context of the FSM (the input band strip) and another interface for the starting states and one more for the end states and so forth. A lot of classes spread out in a lot of files that makes you to quickly lose track of them. There is another way: using enums. Enums are essentially list of classes, and each member of the enum may have a different implementation. suppose we have the following state machine that we want to implement: Init -(‘a’)-> A Init -(else)-> Fail A -(‘b’)-> B A -(‘c’)-> C A -(‘a’)-> A A -(”)-> End A -(else)-> Fail B -(‘c’)-> C B -(‘b’)-> B B -(”)-> End B -(else)-> Fail C -(‘c’)-> C C -(”)-> End C -(else)-> Fail This FSM will validate the following regexp: ^(a+)(b*)(c*)$. We can write the states as elements of the enum State as follows: interface State { public State next(); }class Input { private String input; private int current; public Input(String input) {this.input = input;} char read() { return input.charAt(current++); } } enum States implements State { Init { @Override public State next(Input word) { switch(word.read()) { case 'a': return A; default: return Fail; } } }, A { @Override public State next(Input word) { switch(word.read()) { case 'a': return A; case 'b': return B; case 'c': return C; case '': return null; default: return Fail; } } }, B { @Override public State next(Input word) { switch(word.read()) { case 'b': return B; case 'c': return C; case '': return null; default: return Fail; } } }, C { @Override public State next(Input word) { switch(word.read()) { case 'c': return C; case '': return null; default: return Fail; } } }, Fail { @Override public State next(Input word) { return Fail; } }; public abstract State next(Input word); }What we’ve done is we’ve defined the transitions of every state inside each enum. Each transition returns a new state and as such we can cycle through them more efficiently: State s; Input in = new Input("aabbbc"); for(s = States.Init; s != null || s != States.Fail; s = s.next(in)) {} if(s == null) {System.out.printlin("Valid!");} else {System.out.println("Failed");}At this point, we have either validated the string or failed. It is a simple and elegant design. We could further improve the implementation by separating the final states from the main ones as to simplify the exit condition of the traversal. We will define another interface called FinalState and a second enum that would contain the desired exit states (changing also the States enum accordingly): interface FinalState extends State {} enum FinalStates implements FinalState { Fail { @Override public State next(Input word) { return Fail; } }, Ok { @Override public State next(Input word) { return End; } } }As such, the traversal will be somewhat simplified: for(s = States.Init; !(s instanceof FinalState); s = s.next(in)) {} switch(s) { case Fail: System.out.printlin("Failed"); break; case Ok: System.out.println("Valid!"); break; default: System.out.println("Undefined"); break; }The advantage is that (besides a simpler traversal) we could specify more than two final states. Most of the times, a FSM will have more than one exit point so this last improvement is recommended for the long-term. You can also place all these enums and interfaces inside the same source file and have the whole logic in one place rather than surf through multiple tabs in order to understand how the flow works. As a conclusion, using enums makes for a more compact and meaningful implementation of a FSM. You have all the logic in one file and all traversal is painless. You will also have a much more easier debugging experience since the name of the states in which you’ve translated will appear in the debugger ( the variable s will change its value accordingly, bearing the name of the new state) rather have to figure out what class you have just jumped to. All in all I think it’s a good technique to have in mind. Reference: Automaton implementation in Java from our JCG partner Attila-Mihaly Balazs at the Transylvania JUG blog....
jsf-logo

Spring & JSF integration: Navigation

This is the first in what I hope will be a series of blogs about my efforts to provide deep integration between Spring and JavaServer Faces. Everything mentioned here is a “work in progress” so if you checkout the code please be aware that it is a moving target; expect some rough edges and don’t be surprised if it’s sometimes broken. You can already use Spring with JSF pretty happily out of the box, with Spring managing your beans and JSF handling your screens. There is also some really great support for JSF in Spring Web Flow, if you are doing any flow based application you really should be using Web Flow. Web Flow also provides the org.springframework.faces.mvc.JsfView class that will let you render a JSF page from Spring MVC. Unfortunately JsfView only renders transient (stateless) views, if you want to handle postbacks you are out of luck. Allowing Spring MVC to render JSF views that can handle postback has been my primary driver for starting this project. Thanks to the flexibility of both MVC and JSF it is entirely possible to integrate these technologies (although the exact details of how are probably best saved for another post). I want to spend the rest of this post talking about how we can create really nice JSF navigation. If you have used standard JSF navigation you are probably used to the following type of thing in your faces-config.xml: <navigation-rule> <from-view-id>/pages/list.xhtml</from-view-id> <navigation-case> <from-outcome>select</from-outcome> <to-view-id>/pages/details.xhtml</to-view-id> <redirect/> </navigation-case> </navigation-rule>Whilst it is pretty easy to understand, there are some obvious drawbacks of the standard approach, for starters its pretty verbose. Most of the time I want to redirect my users rather than leaving them confused as to why the URL shows something different to their current page. Needing that <redirect/> on virtually every element is really annoying. The amount of XML obviously upset the developers of JSF themselves, and luckily JSF 2.0 has introduced the concept of implicit navigation. This is something we will make use of later. If you want to read a really good article about JSF navigation checkout Fluent Navigation in JSF 2 by Dan Allen. Navigation is really all about destinations, there is not much point in redirecting someone to a 404 page not found error. Creating nice readable URL destinations has always been a bit of a struggle for JSF. Right now, without developing your own code, the best option for creating readable URLs is probably to use PrettyFaces. Of course, with JSF and Spring integrated nicely you don’t need to use anything other than the @RequestMapping annotation to create readable URLs. The example below shows a how you can map a nice readable URL to show hotel details from an ID. @Controller public class HotelsController { @RequestMapping(value = "/hotels/{id}", method = RequestMethod.GET) public String show(@PathVariable Long id, Model model) { model.addAttribute(bookingService.findHotelById(id)); return "hotels/show"; } }With @RequestMapping annotations in place we can think again about the navigation. Usually the <h:commandButton>, <h:button>, <h:commandLink> or <h:link> components will be used to trigger navigation, for example: <h:commandButton value="Go" action="select">Here when the user clicks on the "Go" button the "select" action kicks in and the navigation rules are used to find a destination. As we want to move away from defining navigation XML we need an alternative approach to find MVC destinations. Slightly subverting JSFs support for implicit navigation gives us quite a nice way to do this. With a bit of integration code we can support a special "spring:" prefix that tells JSF to resolve destinations using Spring MVC. <h:commandButton value="Go" action="spring:redirect:/spring/hotels/123"/>The example above will resolve "redirect:/spring/hotel/123" using the ViewResolvers registered with Spring MVC. In this case UrlBasedViewResolver will pick up "redirect:" and a RedirectView will be used. That’s quite nice, but hard-coding the hotel ID "123" into the view name is not all that practical. Luckily there is an answer: <h:commandButton value="Go" action="spring:redirect:/spring/hotels/{id}"> <f:param name="id" value="#{resultItem.id}/> </h:commandButton>All <f:param> child tags of the commandButton will be used to construct a model for the MVC view. In this case we get a model containing “id=#{resultItem.id}“. The EL value expression #{resultItem.id} will be resolved before the view is rendered. The RedirectView class in Spring 3.1 will deal with URL template variables, so “/spring/hotels/{id}” will pick up “id” to render the complete URL. One slight irritation with the above method is that you need to define your URLs inside your XHTML files as well as in your @RequestMapping annotations. As an alternative to this you can use a special “@bean.method” notation to indicate that you want to navigate to the value of the @RequestMapping on the specified controller bean method: <h:commandButton value="Go" action="spring:@hotelsController.show"> <f:param name="id" value="#{resultItem.id}/> </h:commandButton>If you have more than one @RequestMapping method on your controller bean you can navigate between them using the even shorter syntax “@method” (here the bean is assumed to be the current handler). Of course not every type of @RequestMapping can be reversed into a URL, for example if you use wildcards then this will not work. The advice is to keep your mappings as simple as possible. One final benefit of this method is that we can also reverse the DataBinder process. For example: public class SearchCriteria implements Serializable { private String searchString; private int page; // ... getters / setters }@RequestMapping(value = "/hotels") public String list(SearchCriteria criteria, Model model) { // ... }<h:link outcome="spring:@list"> <f:param name="sc" value="#{searchCriteria}"/> </h:link>Assuming the #{searchCriteria} EL expression resolves to a SearchCriteria object containing the string "California" and the integer 10 the URL built would be "/spring/hotels?searchString=California&page=10". If you would like to have a look at code for this project it is currently available at http://github.com/philwebb/springfaces. As mentioned at the top of the post this code is a work in progress so please expect some problems. My next task on the roadmap is to support a @NavigationMapping annotation that will allow programmatic navigation. Reference: Integrating Spring & JavaServer Faces : Navigation from our JCG partner Phillip Webb at the Phil Webb’s Blog....
apache-commons-logo

Common and Unique Elements In Multiple Collections

This week, we’ll take a break from higher level problems and technology posts to deal with just a little code problem that a lot of us have probably faced. It’s nothing fancy or too hard, but it may save one of you 15 minutes someday, and occasionally it’s nice to get back to basics. So let’s get down to it. On occasion, you’ll find you need to determine which elements in one collection exist in another, which are common, and/or which don’t exist in another collection. Apache Commons Collections has some some utility methods in CollectionUtils that are useful, notably intersection(), but this post goes a bit beyond that into calculating unique elements in collection of collections, and it’s always nice to get down to the details. We’ll also make the solution more generic by supporting any number of collections to operate against, rather than just two collections as CollectionUtils does. Plus there’s the fact that not all of us choose to or are able to include libraries just to get a couple useful utility methods. When dealing with just two collections, it’s not a difficult problem, but not all developers are familiar with all the methods that java.util.Collection defines, so here is some sample code. They key is using the retainAll and removeAll methods together to build up the three sets – common, present in collection A only, and present in B only. Set<String> a = new HashSet<String>(); a.add("a"); a.add("a2"); a.add("common");Set<String> b = new HashSet<String>(); b.add("b"); b.add("b2"); b.add("common");Set<String> inAOnly = new HashSet<String>(a); inAOnly.removeAll(b); System.out.println("A Only: " + inAOnly );Set<String> inBOnly = new HashSet<String>(b); inBOnly .removeAll(a); System.out.println("B Only: " + inBOnly );Set<String> common = new HashSet<String>(a); common.retainAll(b); System.out.println("Common: " + common);Output: A Only: [a, a2] B Only: [b, b2] Common: [common1]Handling Three or More Collections The problem is a bit more tricky when dealing with more than two collections, but it can be solved in a generic way fairly simply, as shown below: Computing Common Elements Computing common elements is easy, and this code will perform consistently even with a large number of collections. public static void main(String[] args) { List<String> a = Arrays.asList("a", "b", "c"); List<String> b = Arrays.asList("a", "b", "c", "d"); List<String> c = Arrays.asList("d", "e", "f", "g");List<List<String>> lists = new ArrayList<List<String>>(); lists.add(a); System.out.println("Common in A: " + getCommonElements(lists));lists.add(b); System.out.println("Common in A & B: " + getCommonElements(lists));lists.add(c); System.out.println("Common in A & B & C: " + getCommonElements(lists));lists.remove(a); System.out.println("Common in B & C: " + getCommonElements(lists)); }public static <T> Set<T> getCommonElements(Collection<? extends Collection<T>> collections) {Set<T> common = new LinkedHashSet<T>(); if (!collections.isEmpty()) { Iterator<? extends Collection<T>> iterator = collections.iterator(); common.addAll(iterator.next()); while (iterator.hasNext()) { common.retainAll(iterator.next()); } } return common; }Output: Common in A: [a, b, c] Common in A & B: [a, b, c] Common in A & B & C: [] Common in B & C: [d]Computing Unique Elements Computing unique elements is just about as straightforward as computing common elements. Note that this code’s performance will degrade as you add a large number of collections, though in most practical cases it won’t matter. I presume there are ways this could be optimized, but since I haven’t had the problem, I’ve not bothered tryin. As Knuth famously said, “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil”. public static void main(String[] args) { List<String> a = Arrays.asList("a", "b", "c"); List<String> b = Arrays.asList("a", "b", "c", "d"); List<String> c = Arrays.asList("d", "e", "f", "g");List<List<String>> lists = new ArrayList<List<String>>(); lists.add(a); System.out.println("Unique in A: " + getUniqueElements(lists));lists.add(b); System.out.println("Unique in A & B: " + getUniqueElements(lists));lists.add(c); System.out.println("Unique in A & B & C: " + getUniqueElements(lists));lists.remove(a); System.out.println("Unique in B & C: " + getUniqueElements(lists)); }public static <T> List<Set<T>> getUniqueElements(Collection<? extends Collection<T>> collections) {List<Set<T>> allUniqueSets = new ArrayList<Set<T>>(); for (Collection<T> collection : collections) { Set<T> unique = new LinkedHashSet<T>(collection); allUniqueSets.add(unique); for (Collection<T> otherCollection : collections) { if (collection != otherCollection) { unique.removeAll(otherCollection); } } }return allUniqueSets; }Output: Unique in A: [[a, b, c]] Unique in A & B: [[], [d]] Unique in A & B & C: [[], [], [e, f, g]] Unique in B & C: [[a, b, c], [e, f, g]]That’s all there is to it. Feel free to use this code for whatever you like, and if you have any improvements or additions to suggest, leave a comment. Developers all benefit when we share knowledge and experience. Reference: Computing Common and Unique Elements In Multiple Collections – Java & from our JCG partner Craig Flichel at the Carfey Software Blog blog....
software-development-2-logo

Diversity in Open Source Projects

I’ve been talking a lot about diversity lately. There are, of course, different kinds of diversity; but when I talk about diversity, I tend to mean diversity in the organizations contributing to an open source project: multiple organizations from different parts of the industry working together. Doug tweeted recently on the topic: Why do you need diverse projects? So when the vendor removes all it’s people, you still have a live project. Obvious, no? I can’t argue with this. But that can’t be the entire answer. Why would any organization care whether or not a project lives on after they pull their resources? One obvious answer is that they may still actually need the software. In that case–even though an organization needs to divest in terms of contributions–they still need to consume the output. Hypothetically then, growing diversity is important for a organization that’s pulling their resources. I suspect, however, that this is rarely the case. If an organization has an interest in consuming the output of the project, they have a very real need to stay involved with that project. At least in some capacity. For an organization, the reasons for increasing diversity are more about the steady state: the day-to-day operation of the project. Diversity is a great way to ensure that a project will survive through periods of time when some of the project contributors are distracted by other interests. Having more than one organization involved in an open source project means that the project will be able to weather periods of inactivity from some subset of the contributing companies. The project can survive a temporary lull in contribution. Diversity also means a shared burden. Resolving conflicts between the interests of multiple organizations participating in a single open source project can be a real challenge. It takes work to make a diverse team function. But when it works, a single organization can get a lot more value of out a project than they put into. It’s one of those games where everybody wins (even if you do get checked hard into the board every once in a while). The act of balancing the different interests of multiple organizations makes a code base stronger. Code bases must generalize to accommodate the use cases of everybody involved; APIs must stabilize; and schedules must be set and followed. In short, everything gets very predictable when multiple diverse interests collaborate. I don’t mean to suggest that all this goodness cannot happen without diversity, but diversity increases the probability. I have watched this happen many times. It may be the case that organizations do not care whether or not a project continues after they divest in it (organizations aren’t people after all ?). But I know from experience that individual committers have trouble letting go. There’s a great deal of pride at stake. Everybody wants be remembered as one of the group who created a great open source project; nobody wants to be remembered as being part of the group that abandoned and let die a great open source project. So maybe that’s enough. Open source project contributors want to have diversity because it’s the best hope of seeing their baby grow into adulthood. Reference: Diversity in Open Source Projects from our JCG partner Wayne Beaton at the Eclipse Hints, Tips, and Random Musings blog....
jcg-logo

Best Of The Week – 2012 – W11

Hello guys, Time for the “Best Of The Week” links for the week that just passed. Here are some links that drew Java Code Geeks attention: * Secure Code Development: A Casualty With Agile?: This article discusses the creation of secure code by Agile teams and references a study that shows that Agile teams do not take security seriously even when building systems which are accessible over the web. Teams could use incremental Attack Surface Analysis to watch for changes in system’s security risk profile. Also check out Essential Attack Surface Management and Simple Security Rules. * Rabbit holes: Why being smart hurts your productivity: An interesting article on how us geeks, in our pursuit of perfection and perfect information get distracted from the task at hand. Fortunately, this can be a weakness and a strength. * GitHub Compromised by Mass Assignment Vulnerability: This article explains briefly how GitHub was compromised by the mass assignment vulnerability. Mass assignment is a technique for mapping form data to objects, essentially for data binding, which unfortunately leaves security holes if no care is taken. Also see Hints for writing secure code and Simple Security Rules. * Coding with JRebel – Java forever changed: This article addresses the ways in which JRebel has made an impact on how developers spend their day coding. With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease. * Java Profiler Comparison: A bried comparison of some of the available Java profiling tools, including JVisualVM, JProfiler, YourKit, JProbe and Spring Insight. Also see Profile your applications with Java VisualVM and Spring Insight – Web Application Profiling.* Save Memory by Using String Intern in Java: This article discusses how to save memory in Java by refactoring the data model and mainly by using String interning. This approach keeps every String only once in memory and by reusing String objects we are able to save memory (yet as a tradeoff for performance). Also check out Low GC in Java: Use primitives instead of wrappers. * All about JMS messages: An introductory article on the structure of JMS messages. The various Header fields, Properties and Bodies are explained. Also check out ActiveMQ IS Ready For Prime Time and Spring 3 HornetQ 2.1 Integration Tutorial. * The Economics of Developer Testing: This article discusses unit testing and what might be a healthy level of it. It is correctly mentioned that tests are code too and need to be maintained, so there is a point of diminishing returns on your time investment. * Typesafe Stack 2.0: Scala, Akka, Play: Typesafe has released Typesafe Stack 2.0, an open source platform for building scalable applications in Java and Scala. The Typesafe Stack includes the Scala programming language, the Akka 2.0 event-driven middleware, the Play 2.0 web framework, and various development tools, that integrate seamlessly with existing Java environments. That’s all for this week. Stay tuned for more, here at Java Code Geeks. Cheers, Ilias Tsagklis...
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close