15 Ağustos 2018 Çarşamba

StaX XMLEvent Arayüzü

constructor
Şöyle yaparız.
XMLEventReader xmlEventReader = ...;

while (xmlEventReader.hasNext()) {

   XMLEvent xmlEvent = xmlEventReader.nextEvent();
   ...
}
isEndElement metodu
Şöyle yaparız.
if (xmlEvent.isEndElement()) {
  EndElement endElement = xmlEvent.asEndElement();
  if(endElement.getName().getLocalPart().equalsIgnoreCase("customer")){
    // all data for the current Customer has been read
    // do something with the customer, like logging it or storing it in a database
    // after this the customer variable will be re-assigned to the next customer
  }
}
isStartElement metodu
Şöyle yaparız.
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()) {

  StartElement startElement = xmlEvent.asStartElement();

  if (startElement.getName().getLocalPart().equalsIgnoreCase("customer")) {
    // start populating a new customer
    customer = new Customer();

    // read an attribute for example <customer number="42">
    Attribute attribute = startElement.getAttributeByName(new QName("number"));
    if (attribute != null) {
      customer.setNumber(attribute.getValue());
    }
  }

  // read a nested element for example:
  // <customer>
  //    <name>John Doe</name>
  if(startElement.getName().getLocalPart().equals("name")){
    xmlEvent = xmlEventReader.nextEvent();
    customer.setName(xmlEvent.asCharacters().getData());
  }
}

StAX XMLEventReader Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.xml.stream.XMLEventReader;
constructor
Şöyle yaparız.
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(
  new FileInputStream(fileName));
hasNext metodu
Şöyle yaparız.
while (reader.hasNext()) {
  XMLEvent event = reader.nextEvent();
  if (event.isEntityReference()) {
    EntityReference ref = (EntityReference) event;
    System.out.println("Entity Reference: " + ref.getName());
  }
}

14 Ağustos 2018 Salı

RMI UnicastRemoteObject Sınıfı - RMI Service Nesnesi Yaratır

exportObject metodu
Normalde nesnemizin java.rmi.server.UnicastRemoteObject'ten kalıtması gerekir. UnicastRemoteObject sınıfı java.rmi.Remote arayüzünden kalıttığı için her metodu uzaktaki bir bilgisayardan çağrılabilir.

Ancak eğer nesnemiz zaten başka bir şeyden kalıtıyorsa ve UnicastRemoteObject'ten kalıtma imkanı yoksa Service nesnesi yaratmak için başka bir şey yapmak gerekir. Bu durumda exportObject() metodunu  kullanırız.

Eğer port numarasını 0 verirsek açıklaması şöyle.
The static method UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients. As a result of the exportObject call, the runtime may begin to listen on a new server socket or may use a shared server socket to accept incoming remote calls for the remote object.
Örnek
Şöyle yaparız.
EchoI echoService = new EchoImpl();
EchoI stub = (EchoI) UnicastRemoteObject.exportObject(echoService, 5000);
Registry registry = LocateRegistry.getRegistry();
registry.bind("echoService", stub);

Stream.collect metodu - Supplier - Kendi Collector Nesnemiz

Giriş
İmzası şöyle.
<R, A> R collect(Collector<? super T, A, R> collector);
Örnek
Hazır collector kullanmak istersek şöyle yaparız.
yourList.stream().collect(Collectors.joining(","))
Örnek
Elimizde bir Collector olsun.
class PackCollector<T> implements Collector<T, List<List<T>>, List<List<T>>> {

  @Override
  public Supplier<List<List<T>>> supplier() {
    return () -> {
      ...
    };
  }

  @Override
  public BiConsumer<List<List<T>>, T> accumulator() {
    return (list, s) -> {
      ...
    };
  }

  @Override
  public BinaryOperator<List<List<T>>> combiner() {
    return (left, right) -> {
      ...
    };
  }

  @Override
  public Set<Characteristics> characteristics() {
    return EnumSet.of(Characteristics.IDENTITY_FINISH);
  }

  @Override
  public Function<List<List<T>>, List<List<T>>> finisher() {
    return Function.identity();
  }

}
Şöyle yaparız.
List<List<String>> result = items.stream().parallel().collect(new PackCollector<>());
System.out.println(result);