mercredi 4 janvier 2017

Issue when importing Sp-PNP-JS

Reg. unable to import sp-pnp-js

I am getting error when importing: import * as spns from "sp-pnp-js";

The error is Error: (SystemJS) Unexpected token < ... SyntaxError: Unexpected token < at eval () at Object.eval ...

In js file i can see this happens when var spns = require("sp-pnp-js");

I have already installed sp-pnp-js

Checking for an object being null in Java

I am writing my code in Java where I am creating an object and and accessing it in two different threads. My first thread1 thread calls some public methods on this object at runtime.

final Thread thread1 = new Thread(){
    @Override
    public void run() {
        myObj.pubFunc1();
        myObj.puFunc2();
        myObj.pubFunc3();
    }
};

I have another thread thread2 which might release this object and set it to nulllike:

final Thread thread2 = new Thread(){
    @Override
    public void run() {
        myObj.release();
        myObj = null;
    }
};

My question is if I should put check for null around each statement in my thread1 like this?

final Thread thread1 = new Thread(){
        @Override
        public void run() {
            if(myObj != null) {
                myObj.pubFunc1();
            }
            if(myObj != null) {
                myObj.pubFunc2();
            }
            if(myObj != null) {
                myObj.pubFunc3();
            }
        }
    };

OR only one check around all the statements is enough? The basic question that might originate from this is there is a lock around the object on which we have made a null check? How to handle this situation. What design pattern should I use to handle this situation.

NOTE: I do not want the three statements to be necessarily executed and I even do no want that the three statements form an atomic unit. I just want to perform an operation if the object I am performing the operation on is not null.

pattern not printing while trying to match same string on two files

I am trying to find pattern from two different files and then need to sum the last values of two filesand then print sum with first ,second and third field(sum) in xls...this is so far i wrote but it is printing value of first if loop not sure why not printing for second.if somone having better logic than below to do same operation ...

file1.txt 
L01B,"ABC",832048921.62
L01E,"DDD",70675364.68
L02A,"ZZZ",19747732853.37

file2.txt
L01B,"AAA",832048921.62
L01E,"DDD",70675364.68
L02A,"ZZZ",19747732853.37
#!/usr/bin/perl

use warnings;
use strict ;
  use Date::Simple qw(d8);

  my $firstdate = $ARGV[0];
  my $d1 = d8($firstdate);
 my $seconddate = $ARGV[1];
  my $d2 = d8($seconddate);
  my $f1=$d1-> format ('%d_%m_%Y');
  my $f2=$d2-> format ('%d_%m_%Y');
  print  " $f1 \t";
  print "$f2\n";
  open(my $fh1, '<', $f1) or die "Could not open file '$f1' $!";
  open(my $fh2, '<', $f2) or die "Could not open file '$f2' $!";

  while ( my $line1 = <$fh1>) {
           chomp $line1;
           if ($line1 =~/L01B/){
               my @array = split /[,]+/,$line1;
               {
               while (my $line2 = <$fh2>)
               {
               if ($line2 =~/L01B/){

               my @array1  = split /[,]+/,$line2;
               print " match found sec $array1[0]\n";
              print " match found sec $array1[1]\n";
              print " match found sec $array1[2]\n";
              print " match found first $array[0]\n";
              print " match found first $array[1]\n";
              print " match found first $array[2]\n";
              }

              }
              }
             }
             if ($line1 =~/L01B/){
               my @array2 = split /[,]+/,$line1;
               {
               while (my $line2 = <$fh2>)
               {
               if ($line2 =~/L01A/){

               my @array3  = split /[,]+/,$line2;
               print " match found sec $array2[0]\n";
              print " match found sec $array2[1]\n";
              print " match found sec $array2[2]\n";
              print " match found first $array3[0]\n";
              print " match found first $array3[1]\n";
              print " match found first $array3[2]\n";
              }

              }
              }

             }

             }
    close($fh1);
    close($fh2);        

mardi 3 janvier 2017

Java - Simple method faster than design pattern?

So i have the Main.class

public class Main {
    public static void main(String[] args) {

        NodoLista<Integer> nodoLista = new NodoLista<Integer>(1);
        nodoLista.app(2);
        nodoLista.app(3);
        nodoLista.app(4);
        nodoLista.app(5);

        long startTime, endTime;

        startTime = System.nanoTime();
        System.out.println(nodoLista.list());
        endTime = System.nanoTime();
        System.out.format("%.2f miliseconds elapsed.\n", (endTime - startTime) * 1.0 );

        startTime = System.nanoTime();
        System.out.println(nodoLista.list2());
        endTime = System.nanoTime();
        System.out.format("%.2f miliseconds elapsed.\n", (endTime - startTime) * 1.0 );

    }

    public Main() {
        super();
    }

}

My NodoLista class:

public class NodoLista<T> {

    private T value;

    private NodoLista<T> siguiente;
    private EstadoSiguiente<T> estadoSiguiente;

    // Getters and Setters

    public NodoLista(T value) {
        this.value = value;
        this.estadoSiguiente = new EstadoSinSiguiente<T>(this);
    }

    public void app(T valor) {
        estadoSiguiente.setSiguiente(valor);
    }

    public ArrayList<T> list() {
        return estadoSiguiente.list();
    }

    public ArrayList<T> list2() {
        ArrayList<T> subList = new ArrayList<T>();
        subList.add(getValue());
        NodoLista<T> actual = getSiguiente();
        while (actual != null) {
            subList.add(actual.getValue());
            actual = actual.getSiguiente();
        }
        return subList;
    }
}

And my state pattern for the next node that i called siguiente:

public interface EstadoSiguiente<T> {

    void setSiguiente(T valor);

    ArrayList<T> list();

}

public class EstadoConSiguiente<T> implements EstadoSiguiente<T> {

    public NodoLista<T> nodo;

    @Override
    public void setSiguiente(T valor) {
        nodo.getSiguiente().app(valor);
    }

    @Override
    public ArrayList<T> list() {
        ArrayList<T> subList = new ArrayList<T>();
        subList.add(nodo.getValue());
        subList.addAll(nodo.getSiguiente().list());
        return subList;
    }

}

public class EstadoSinSiguiente<T> implements EstadoSiguiente<T> {

    public NodoLista<T> nodo;

    @Override
    public void setSiguiente(T valor) {
        nodo.setSiguiente(new NodoLista<T>(valor));
        nodo.setEstadoSiguiente(new EstadoConSiguiente<T>(nodo));
    }

    @Override
    public ArrayList<T> list() {
        ArrayList<T> subList = new ArrayList<T>();
        subList.add(nodo.getValue());
        return subList;
    }

}

Basicly, y have two functions that do the same, list() return a list of elements of te linked list recursively, list2() return exactly the same, procedurally.

I notice that the procedural method just is faster than recursive, not substantially, but,why do this happen? Are messages between classes slower than a block of code, In this case?

which is better design pattern state or observer pattern in object C++

I am just a bit confused with the difference between the observer and state pattern. I have been given a project where the client is an airplane on a flight which calculates data for different sensor such as GPS,Speed, Fuel level, And then the data is sent over to the network and then to the server. At the moment I have used the observer pattern for my class diagram. The class diagram can be viewed from the below, please let me know if my solution is correct and if the design pattern chosen is good for this solution. Class Diagram

Strategy Pattern using Unity

I'm trying to use the Strategy pattern using dependecy injection with Unity, and i have the following scenario:

public Interface IName
{
  string WhatIsYourName()
}

public class John : IName
{
   public string WhatIsYourName()
   {
     return "John";
   } 
}

public class Larry : IName
{
   public string WhatIsYourName()
   {
     return "Larry";
   } 
}

public Interface IPerson
{
   IntroduceYourself();
}

public class Men : IPerson
{
   private IName _name; 

   public Men(IName name)
   {
     _name = name;
   }

   public string IntroduceYourself()
   {
     return "My name is " + _name.WhatIsYourName();
   }
}

How can i set unity container to inject the correct name to the correct person?

Example:

IPerson_John = "code to container resolve";

IPerson_John.IntroduceYouself(); // My name is john

IPerson_Larry = "code to container resolve";

IPerson_Larry.IntroduceYouself(); // My name is Larry

Similar problem: Strategy Pattern and Dependency Injection using Unity . Unfortunately, I cannot use this solution once i have to inject dependecy in the "constructor"

i want to print * pattern with prespaces like..as per below: [on hold]

i want to print * pattern with prespaces like..as per below:

    *
   **
  ***
 ****
*****

need answer. p.s:Just only using of for loop not using any fuction like repeat or any..