Category Archives: java

.equals(), the other way

The new year bring with it a new project for me.
The new project came new colleagues.
And the new colleagues write funny stuff.
One colleague has his own way to check if two strings are the the same:

if (nodeId.compareTo("HEADER") == 0) {
	// ...
}
if (nodeId.compareTo("SHEET") == 0) {
	// ...
}
if (nodeId.compareTo("FOOTER") == 0) {
	// ...
}


Problem created, problem solved

Many thanks to Emanuele for submitting the code.

This is how you create the problem:

   Long.parseLong(Long.toString(id.longValue()));



And this is how you’re going to solve it:

   try {
           mId = Long.parseLong(Long.toString(id.longValue()));
           log.debug("id: " + id.longValue());
       } catch (NullPointerException npEx) {
           log.debug("NullPointerException npEx :: " + npEx.getMessage());
           return ActionSupport.ERROR;
       } catch (NumberFormatException nfEx) {
           log.debug("NumberFormatException nfEx :: " + nfEx.getMessage());
           return ActionSupport.ERROR;
       } catch (Exception gnEx) {
           log.debug("Exception gnEx :: " + gnEx.getMessage());
           return ActionSupport.ERROR;
       }

Someone said “If you are not part of the solution then you are part of the problem”.
The guy who has wrote this chunk of code, eventually is part of both.


The hard way

If you have to initialize a boolean, you can use ‘true’ or ‘false’. It’s easy.

But if you feel a little more fancy you can use something like that:

	boolean result = (IU.getId_profile()+"-"+IU.getId_project()+"-"+IU.getId_LEV())
		  .equals(IU.getId_profile()+"-"+IU.getId_project()+"-"+IU.getId_LEV());


Date Parsing Perversion /4

		public static long parseDate(String month,String day,String time_year)
			throws Exception
		{
			int pos_2p;
			try 
			{
				GregorianCalendar calendar = new GregorianCalendar();
				if((pos_2p=time_year.indexOf(":"))!=-1)
					calendar.set(calendar.get(Calendar.YEAR),
					((Month_STR.indexOf(month)+1+4)/4)-1,
					Integer.parseInt(day),
					Integer.parseInt(time_year.substring(0,pos_2p)),
					Integer.parseInt(time_year.substring(pos_2p+1)),
					0);
				else
					calendar.set(Integer.parseInt(time_year),
					((Month_STR.indexOf(month)+1+4)/4)-1,
					Integer.parseInt(day),
					0,0);
					
				return calendar.getTime().getTime();
				
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			return 0;
		}

.getTime().getTime()


Max

	String InfoClientRS="\n[" +
		"\n[\"MAX15041967\",\""+IQ.getUnique_ID()+"\",\""+IQ.getTitle()+"\",\""+IQ.getName()+"\",\""
		+(CCOL==null?"":CCOL.replace('"', '\''))+"\",\""+IQ.implementFilter()+"\"];

Who is Max??

Actually in the codebase of the big TLC company, I guess (and hope) this code is unused


As promised

… in Condition, here the code contained by the if statement.

It’s a little bit convoluted, but it’s so well commented.

	/**/
	if (cod_tipo_uo.equals("21") || !cod_tipo_uo.equals("21")){
		if (itemComboTip.equals("")){
			ptfType = "";
			ptfSubTtypel = "";
			if (cod_tipo_uo.equals("A3")|| cod_tipo_uo.equals("94")){
				if (!ContextUtil.consumeArea(request, "CodItem").isEmpty() && 
						ContextUtil.consumeArea(request, "CodItem")!= null	){
					String val = (String) ContextUtil.consumeArea(request, "CodItem")
						.toArray() [0];
					val = val.substring(3,val.length());
					ContextUtil.informArea(request, val, "searchType");
	/***/ 
					if (!val.equals("ALL")){					
						ptfType = val.substring(0,2);
						if (val.length()== 5){
							ptfSubTtypel = val.substring(3,5);
						}else if (val.length()== 2){
							ptfSubTtypel = "";
						}	

						if (ptfSubTtypel.equals("")){
							ContextUtil.informArea(request,cod_tipo_uo 
								+'-'+ ptfType, "searchType");					
						}else ContextUtil.informArea(request,cod_tipo_uo 
							+'-'+ ptfType+'-'+ptfSubTtypel, "searchType");
					}				
	/****/							
				}	
			}else ContextUtil.informArea(request, "ALL", "searchType");
		}
	}

Still no clue of what it was about.

(I think) Actually in the production code of the Big Bank, I did not dare touch it


How to iterate

The problem is ‘remove a certain item from a collection’.
If you remove an object and the counter keep going, you’re going to jump the next element.

The solution my predecessor come to:

	private void removeData(String code){
		String sp914code;
			
		for (int i = 0; i < sp914Codes.size(); i++){
			sp914code = sp914Codes.get(i).toString();
			if(sp914code.equals(code)){
				sp914Codes.remove(i);
				i=-1;
			}
		}
	}

Restart everytime from the very beginning, obvious.

I doesn’t dare touch this, even if I’d worked on that class, in THAT Big Bank