16 Ocak 2020 Perşembe

maven pom.xml repositories tag

repositories tag
Maven merkezi repository dışında daha farklı bir repository eklemek istersek kullanırız.

URL Tag
Örnek
Şöyle yaparız.
<repositories>
  <repository>
    <id>my-internal-site</id>
    <url>http://myserver/repo</url>
  </repository>
</repositories>
Örnek
Central Repository için https kullanmak gerekir. Açıklaması şöyle.
Effective January 15, 2020, The Central Repository no longer supports insecure communication over plain HTTP and requires that all requests to the repository are encrypted over HTTPS.
Şöyle yaparız.
<repositories>
   <repository>
      <id>central maven repo</id>
      <name>central maven repo https</name>
      <url>https://repo.maven.apache.org/maven2</url>
   </repository>
</repositories>
Örnek - dosya Sistemi
Dosya sistem için şöyle yaparız.
<repositories>
  <repository>
    <id>local-repo</id>
    <url>file:///${project.basedir}/repo</url>
  </repository>
</repositories>
releases ve snapshots Tag
Örnek 
Şöyle yaparız.
<repositories>
  <repository>
    <id>maven.oracle.com</id>
    <releases> 
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
   <url>https://maven.oracle.com</url>
  </repository>
</repositories>
server tag
servers tag içindedir. Açıklaması şöyle.
Some artifact repositories require a username and password to access. Many times, the repository is a private server hosting internal artifacts. When this is the case, credentials for accessing the repository must be provided in the settings.xml,
Deploy edilecek Nexus şifresi kullanılır.
Örnek
Şöyle yaparız. id alanı ile repository id alanı aynı olmalı.
<profile>
  <repositories>
    <repository>
      <id>...</id>
      ...
    <repository>
  <repositories>
<profile>
<server>     
  <id>myrepo_Snapshots</id>
  <username>admin</username>
  <password>xxxx</password>
</server>

JPA @Lob Anotasyonu

Giriş
Şu satırı dahil ederiz.
import javax.persistence.Lob;
Verinin java.sql.Types.BLOB veya java.sql.Types.CLOB olarak kaydedilmesini sağlar. Açıklaması şöyle. Eğer üye alan tipi String veya char ise java.sql.Types.CLOB seçilir, değilse java.sql.Types.BLOB seçilir.
Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type. Portable applications should use the Lob annotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation or the ElementCollection annotation when the element collection value is of basic type. A Lob may be either a binary or character type.
The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob.
Açıklaması şöyle. Üye alanın Serializable olması gerekir.
@javax.persistence.Lob signifies that the annotated field should be represented as BLOB (binary data) in the DataBase.

You can annotate any serializable data type with this annotation. In JPA, upon persisting (retrieval) the field content will be serialized (deserialized) using standard Java serialization.

Common use of LOB is to annotate a HashMap field inside your Entity to store some of the object properties which are not mapped into DB columns. That way all the unmapped values can be stored in the DB in one column in their binary representation. Of course the price that is paid is that, as they are stored in binary format, they are not searchable using the JPQL/SQL.
Üye Alan Tipi
Eğer portable kod istiyorsak üye alan String veya byte[] olmalıdır. Böylece herhangi bir JPA sağlayıcısını kullanabiliriz.

Veritabanı Sütun Tipi
Kullandığımız ORM bu tipi gerçek veri tabanı sütun tipine çevirir. Ancak veri tabanları arasında da farklılıklar var. Örneğin PostgreSQL oid sütun tipini kullanırken, Oracle kendi blob tipini kullanıyor.
annotation                   postgres     oracle
-------------------------------------------------
byte[] + @Lob                oid          blob 
Not : Hibernate'e Mahsus JPA @Lob Anotasyonu - PostgreSQL yazısına tababilirsiniz.

Örnek - Oracle
Bazen veri tabanında kullanılacak sütun tipini özellikle belirtmek gerekir. Şöyle yaparız. CLOB ve BLOB Oracle sütun tipleridir.
@Lob
@Column(name = "CHARS", columnDefinition = "CLOB")
private String chars;`

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "DATA", columnDefinition = "BLOB", nullable = false)
private byte[] data;
Lazy 
@Lob lazy değildir. Lazy olması için kodda bunu belirtmek gerekir.
Örnek
Şöyle yaparız.
@Basic(fetch = FetchType.LAZY) 
@Lob
private String projectDescription = "";
Örnek
Şöyle yaparız.
@Basic(fetch = FetchType.LAZY)
@Lob
@Column(length=5000)
private byte[]  projectDescription1 =new byte[0];

15 Ocak 2020 Çarşamba

Awt Color Sınıfı

cosntructor - int
32 bit'in sadece ilk 24 bitini kullanır.
Örnek
Şöyle yaparız. Bu koddatoplam 28 bit çarpım yapılıyor.  6 tane 0 sıfır var.
new Color((int)(Math.random() * 0x1000000))

getHSBColor metodu
Örnek ver

14 Ocak 2020 Salı

PowerMockito Whitebox Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.powermock.reflect.Whitebox;
getInternalState metodu
Sınıfın private alanının değeri alınabilir.

invokeConstructor metodu
Private builder içeren sınıf yaratılabilir.

invokeMethod metodu
Private metod çağrılabilir.

setInternalState metodu
Public olmayan alana değer atanabilir.
Örnek - static alan
Elimizde şöyle bir kod olsun
public class Foo {
  private static Bar b;
}
Bu alan static olduğu için şöyle yaparız. Buradar mockBar Bar tipinden olduğu için ve Bar tipinden tek alan olduğu için sıkıntık çıkmıyor.
Whitebox.setInternalState(Foo.class, mockBar);
Eğer Bar tipinden iki tane alan olsaydı alan ismini kullanmak gerekirdir. Bu durumda şöyle yaparız
Whitebox.setInternalState(Foo.class,"bar", mockBar);

12 Ocak 2020 Pazar

Instant Sınıfı

Giriş
Şu satırı dahil ederiz.
import java.time.Instant;
Açıklaması şöyle
The Instant class is always in UTC by definition.
Açıklaması şöyle
The Instant class is a basic building-block class, indicating a moment on the timeline in UTC. Usually when performing manipulations such as adding hours, you’ll likely want to account for anomalies such as Daylight Saving Time, and so you will care about time zone. For that, use the ZonedDateTime class.
Bu sınıf UTC zaman çizgisindeki bir noktayı temsil eder. Eski Date ve Calendar sınıfları milisaniye seviyesinde çözünürlük sağlarken bu sınıf nanosaniye seviyesinde çözünürlük sağlayabilir.

Instant yerine UTC zaman dilimini kullanan ZonedDateTime da kullanılabilir. Şöyle yaparız.
ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC);
atZone metodu
Instant Nesnesinden ZonedDateTime Nesnesine Çevrim yazısına taşıdım

getEpochSecond metodu
Açıklaması şöyle. Yani getEpochSecond () + getNano() epoch'tan beri geçen tüm süreyi ifade eder.
The range of an instant requires the storage of a number larger than a long. To achieve this, the class stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999.
Şöyle yaparız.
long unixTimestamp = Instant.now().getEpochSecond();
getNano metodu
Örnek ver

isBefore metodu
Örnek
Şöyle yaparız.
Instant now = Instant.parse("2018-06-21T08:40:00.00Z");
Instant then = now.minus(9, ChronoUnit.HOURS);

if (then.isBefore(now)) {
  ...
}
minus metodu
Örnek
Şöyle yaparız.
// two weeks ago
long pastTimestamp = Instant.now().minus(14, ChronoUnit.DAYS).getEpochSecond();
Örnek
Şöyle yaparız.
Instant now = Instant.parse("2018-06-21T08:40:00.00Z");
Instant then = now.minus(9, ChronoUnit.HOURS);
now metodu
Şöyle yaparız.
Instant now = Instant.now();
Veritabanına UTC saati yazmak için şöyle yaparız.
myPreparedStatement.setObject( … , Instant.now() ) ;
now metodu - Clock
Şöyle yaparız.
Instant now = Instant.now ( Clock.systemUTC () );
Instant now = Instant.now ( Clock.system ( ZoneId.of ( "America/Montreal" ) ) );
Instant now = Instant.now ( Clock.systemDefaultZone () );
Hepsi aynı sonucu verir.
instantClockSystemUTC.toString(): 2016-12-31T20:52:39.763Z
instantClockSystem.toString(): 2016-12-31T20:52:39.763Z
instantClockSystemDefaultZone.toString(): 2016-12-31T20:52:39.763Z
ofEpochSecond metodu - saniye
Belirtilen saniyeden bir Instant nesnesi oluşturur.
Örnek
Şöyle yaparız.
Instant instant = Instant.ofEpochSecond( 1220227200L);
Örnek
Şöyle yaparız
System.out.println(Instant.ofEpochSecond(1512431637067L));
// far future: +49897-01-18T19:11:07Z
ofEpochSecond metodu - saniye + nanosaniye
Örnek
Mikrosaniyeden yaratmak için şöyle yaparız.
Instant.ofEpochSecond(TimeUnit.MICROSECONDS.toSeconds(microseconds),    
    TimeUnit.MICROSECONDS.toNanos(microseconds)        
    - 
    TimeUnit.SECONDS.toNanos(TimeUnit.MICROSECONDS.toSeconds(microseconds)
 
)              
ofEpochMili metodu
Belirtilen milisaniyeden bir Instant nesnesi oluşturur. Metodun içi şöyle.
public static Instant ofEpochMilli(long epochMilli) {
  long secs = Math.floorDiv(epochMilli, 1000);
  int mos = (int)Math.floorMod(epochMilli, 1000);
  return create(secs, mos * 1000_000);
}
Örnek
Şöyle yaparız.
System.out.println(Instant.ofEpochMilli(1512431637067L));
// output: 2017-12-04T23:53:57.067Z
parse metodu
Örnek
Şöyle bir string'i başarıyla çevirir.
2016-10-21T00:00:00Z
Örnek
Şöyle yaparız.
Instant now = Instant.parse("2018-06-21T08:40:00.00Z");
plus metodu
Bu sınıfın plusHours() diye bir metodu yok. Oracle örnekler yanlış. Şöyle yaparız.
Instant later = instant.plus( 1 , ChronoUnit.HOURS ) ;
toString metodu
Çıktı olarak Yıl+Ay+Gün+Saat+Dakika+Saniye(double) verir.
Örnek
Şöyle yaparız.
Instant instant = Instant.now (); // Current date-time in UTC.
String output = instant.toString ();
Çıktı olarak şunu alırız.
2016-05-06T23:24:25.694Z
Örnek
Şöyle yaparız.
System.out.println(Instant.ofEpochMilli(1512431637067L));
// output: 2017-12-04T23:53:57.067Z
truncatedTo metodu
Örnek
Şöyle yaparız.
import static java.time.temporal.ChronoUnit.DAYS;

final Map<Instant, List<Entry>> entries =
    list.stream().collect(Collectors.groupingBy(request -> 
        request.getDate().toInstant().truncatedTo(DAYS)));
Çıktı olarak şunu alırız.
2011-03-21
    VALUE1
2011-03-21
    VALUE2
2011-03-22
    VALUE3
    VALUE4
2011-03-21
    VALUE5
Örnek
Şöyle yaparız.
Instant start = ...;
Instant target = ...;

if (!target.truncatedTo(ChronoUnit.DAYS).equals(start.truncatedTo(ChronoUnit.DAYS))) {
  ...
}