samedi 25 avril 2015

Android Studio – Which .java file is executed when I run tests?


Where can I choose which .java to execute when running an Android Studio test?

I have 3 .java files and only one is being executed.

When searching the project for references for the specific .java which runs every time, I've only found the file name in workspace.xml file which is probably irrelevant.


What is the LocalDate Utils like StringUtils


  private ClientIdentifier(final Client client, final CodeValue documentType, final String documentKey,final LocalDate validity, final String description) {
    this.client = client;
    this.documentType = documentType;
    this.documentKey = StringUtils.defaultIfEmpty(documentKey, null);
    i am confused in this part //this.validity = LocaleUtils.defaultIfEmpty(validity,null);
    this.description = StringUtils.defaultIfEmpty(description, null);
}

I am passing LocalDate but don't know which utility to use for checking defualtIfEmpty


jasper subreport is not getting printed inside main report


i have mainreport and subreport , where main report is getting printed in jasper, but not the subreport, see below code for subreport

<jr:list xmlns:jr="http://ift.tt/1cl9b3L" xsi:schemaLocation="http://ift.tt/1cl9b3L http://ift.tt/1cl9b3P" printOrder="Vertical">
                    <datasetRun subDataset="customerOrderList" uuid="38a2e0f4-ae87-42ad-b3b4-61853d9a2e23">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{customerOrderList})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="39" width="590">
                        <frame>
                            <reportElement x="0" y="0" width="590" height="39" uuid="609e3e45-03f6-48d0-8776-e6e393d0dbaf"/>
                            <subreport>
                                <reportElement x="0" y="0" width="590" height="39" uuid="d4ef2aed-7826-49e5-9bb1-7642cbd01279"/>
                                <subreportExpression><![CDATA["src/main/java/com/crafart/reports/customerOrders.jasper"]]></subreportExpression>
                            </subreport>
                        </frame>
                    </jr:listContents>
                </jr:list>

i have debugged the code and see there are no issues in generating the report.

i am passing customer order which contains customer details and list of orders. snippet of my customer bean

public class Customer {
    private String customerName;
    private List<Order> customerOrderList
}

and below code used to generate the report

    List<OrderDeclarationBO> orderDeclarationBOLst = new ArrayList<>();
    orderDeclarationBOLst.add(orderDeclarationBO);
    InputStream in = this.getClass().getResourceAsStream("order.jrxml");
    InputStream jasperIn = this.getClass().getResourceAsStream("order.jasper");
    JasperCompileManager.compileReport(in);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperIn, new HashMap<String, Object>(), new JRBeanCollectionDataSource(orderDeclarationBOLst));
    JasperExportManager.exportReportToPdfFile(jasperPrint, "orderInvoice.pdf");

need help, and dont know what is still required to print subreport.

EDIT :- Adding image of the report, where i am getting null values inside sub report, and not aligned properly

enter image description here


How to use Observable java


I have the following:

abstract class A extends Class C (abstract class C extends D).

private String name="";

public void setFff(double val) {

    double old = fff.getAvg();
    fff.update(val);

    ListIterator<Observer> it = fffListeners.listIterator();
    String ss;

    while (it.hasNext()) {
      ss = name + this.toString();
      it.next().update("fff", fff.getValue(), fff.getAvg(), ss);
    }
}

class B extends D implements Observable

private String name="";
public void notifyObservers() {
        for(Observer observer:fffListeners){
            v=fff.getValue();
             m=fff.getAvg();
            observer.update("fff", v, m, (name+this.getName()));
        }       
    }

interface Observer:

void update(String nn, double bbb, double aaa, String nameThraed);

abstract class E implements Observer

class G extends E.

update in class G is the following

public void update(String string, double v, double m, String s) {

        if ((B.getClass == "G")
                && string.equals("fff")&& (s.equals("B"))) {

             this.fffList.add(m);
            System.out.println("List of fff :" + fffList);

        }
    myMethod(fffList);
}

I want to get all the value of m and put theme in the list fffList when I get a notification from E. How can i do that?


Java how does HashMap identify the key object if hashCode are the same and equals is true?


I always thought hat HashMap is identifying its keys by using hasCode and equals. But this code still does not give me the value back. What am I missing?

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kic on 25.04.15.
 */
public class TypePair {
    private final Class a;
    private final Class b;

    public TypePair(Class a, Class b) {
        this.a = a;
        this.b = b;
    }

    public TypePair(Object a, Object b) {
        this.a = a.getClass();
        this.b = b.getClass();
    }

    public boolean isPair (Object a, Object b) {
        return this.a.isAssignableFrom(a.getClass()) && this.b.isAssignableFrom(b.getClass());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TypePair) {
            return a.isAssignableFrom(((TypePair) obj).a) && b.isAssignableFrom(((TypePair) obj).b);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return 1;
    }

    public static void main(String[] args) {
        TypePair a = new TypePair(Number.class, String.class);
        TypePair b = new TypePair(12, "hello");

        Map<TypePair, Boolean> test = new HashMap<>();
        test.put(a, true);

        System.out.println(a.hashCode() == b.hashCode());
        System.out.println(a.equals(b));
        System.out.println("und? " + test.get(a));
        System.out.println("und? " + test.get(b));
    }
}


Prints
true
true
und? true
und? null


SimpleXML java staying in strict mode even if I use strict/required=false


I am trying to use SimpleXML in non strict mode but the only thing that seems to work is to put required=false on every single field. Using false in read or in @Root doesn't seem to turn off strict mode.

MyClass myclass = serializer.read(MyClass.class, new File(args[0]), false);

Results in..

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(name=, required=true, empty=)

I am using SimpleXML java v2.7.1 under Java 1.8.

Any ideas why?


Why can there be an ArrayOutOfBounds Exception when you are opening a file in java using FileInputStream?


Why is there a need to check ArrayIndexOutOfBounds in FileInputStream in java? This same code is given as an example in a book for core java.

FileInputStream fis;
try{
    fis=new FileInputStream("inputFIle.txt");       
}catch(FileNotFoundException e){
    System.out.println("File was not found");
}catch(ArrayIndexOutOfBoundsException e){
    e.printStackTrace();
}