Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common module #4

Merged
merged 26 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fed1fc6
Add lombok dependencies
chimp1984 Jun 12, 2021
74abbe8
Add i18n module
chimp1984 Jun 12, 2021
a835008
Add `out` to gitignore
chimp1984 Jun 12, 2021
fdf1d93
Add common module
chimp1984 Jun 12, 2021
7f5c8b2
Update i18n/src/main/java/network/misq/i18n/UTF8Control.java
chimp1984 Jun 12, 2021
8a540a8
Use use try-with-resources for stream.
chimp1984 Jun 12, 2021
8fcc66b
Update common/src/main/java/network/misq/common/currency/FiatCurrency…
chimp1984 Jun 12, 2021
c678f79
Update common/src/main/java/network/misq/common/currency/MisqCurrency…
chimp1984 Jun 12, 2021
31fac5f
Update common/src/main/java/network/misq/common/locale/LocaleReposito…
chimp1984 Jun 12, 2021
bf6129b
Update common/src/main/java/network/misq/common/locale/LocaleReposito…
chimp1984 Jun 12, 2021
f7298d2
Update common/src/main/java/network/misq/common/locale/Region.java
chimp1984 Jun 12, 2021
0660ecd
Update common/src/main/java/network/misq/common/util/FileUtils.java
chimp1984 Jun 12, 2021
6dbea94
Update common/src/main/java/network/misq/common/util/MapUtils.java
chimp1984 Jun 12, 2021
1ceaab6
Revert change as we do not have apache dependency yet, and that case …
chimp1984 Jun 12, 2021
1d60006
Add java doc
chimp1984 Jun 12, 2021
071abe7
Add java doc and rename list to make it more clear that it expects a …
chimp1984 Jun 12, 2021
132e74a
Break up if/else branches
chimp1984 Jun 12, 2021
4e5f225
Fix incorrect map access
chimp1984 Jun 14, 2021
b6a682b
Merge branch 'add_i18n_module' into add_common_module
chimp1984 Jun 14, 2021
6edd97c
Remove reference to common-platform
ghubstan Jun 21, 2021
5db7a1a
Remove logback.xml from i18n module
ghubstan Jun 21, 2021
a390c87
Merge branch 'add_i18n_module' into add_common_module
ghubstan Jun 21, 2021
5c3df36
Move logback.xml from src/rescources to test/resources
ghubstan Jun 21, 2021
0aee3d2
Replace Logger declaration with lombok @Slf4j annotation
ghubstan Jun 21, 2021
f6a84d6
Suppress IDE code analysis warning
ghubstan Jun 21, 2021
b972eda
Do null check on inputStream
ghubstan Jun 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ hs_err_pid*
.DS_Store
.gradle
build

out
7 changes: 7 additions & 0 deletions buildSrc/lombok-dependencies.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
api platform(project(':platforms:common-platform'))
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}
5 changes: 5 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ repositories {
apply from: '../buildSrc/misq-version.gradle'
apply from: '../buildSrc/logging-dependencies.gradle'
apply from: '../buildSrc/test-dependencies.gradle'
apply from: '../buildSrc/lombok-dependencies.gradle'

dependencies {
api platform(project(':platforms:common-platform'))

implementation 'com.google.guava:guava'
}

test {
useJUnitPlatform()
exclude '**/**Integration*'
}
22 changes: 22 additions & 0 deletions common/src/main/java/network/misq/common/Disposable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common;

public interface Disposable {
void dispose();
}
42 changes: 42 additions & 0 deletions common/src/main/java/network/misq/common/ObjectSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common;

import java.io.*;

public class ObjectSerializer {
public static Serializable deserialize(byte[] bytes) {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
ObjectInput in = new ObjectInputStream(bis);
return (Serializable) in.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public static byte[] serialize(Serializable object) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(object);
objectOutputStream.flush();
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.currency;


import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@EqualsAndHashCode(callSuper = true)
@ToString
public final class CryptoCurrency extends MisqCurrency {
// http://boschista.deviantart.com/journal/Cool-ASCII-Symbols-214218618
private final static String PREFIX = "✦ ";

@Getter
private final boolean isAsset;

public CryptoCurrency(String currencyCode,
String name) {
this(currencyCode, name, false);
}

public CryptoCurrency(String currencyCode,
String name,
boolean isAsset) {
super(currencyCode, name);

this.isAsset = isAsset;
}

@Override
public String getDisplayPrefix() {
return PREFIX;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.currency;


import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.Currency;
import java.util.Locale;

@EqualsAndHashCode(callSuper = true)
@ToString

chimp1984 marked this conversation as resolved.
Show resolved Hide resolved
public final class FiatCurrency extends MisqCurrency {
// http://boschista.deviantart.com/journal/Cool-ASCII-Symbols-214218618
private final static String PREFIX = "★ ";

@Getter
private final Currency currency;

public FiatCurrency(String currencyCode, Locale locale) {
this(Currency.getInstance(currencyCode), locale);
}

public FiatCurrency(Currency currency, Locale locale) {
super(currency.getCurrencyCode(), currency.getDisplayName(locale));

this.currency = currency;
}

@Override
public String getDisplayPrefix() {
return PREFIX;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.currency;

import network.misq.common.locale.CountryRepository;
import network.misq.common.locale.LocaleRepository;

import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;

public class FiatCurrencyRepository {
private static Map<String, FiatCurrency> fiatCurrencyByCode;

static {
applyLocale(LocaleRepository.getDefaultLocale());
}

// Need to be called at application setup with user locale
public static void applyLocale(Locale locale) {
fiatCurrencyByCode = CountryRepository.COUNTRIES.stream()
.map(country -> getFiatCurrencyByCountryCode(country.code(), locale))
.distinct()
.collect(Collectors.toMap(FiatCurrency::getCode, Function.identity(), (x, y) -> x, HashMap::new));
}

public static FiatCurrency getFiatCurrencyByCountryCode(String countryCode, Locale locale) {
if (countryCode.equals("XK")) {
return new FiatCurrency("EUR", locale);
}

Currency currency = Currency.getInstance(new Locale(locale.getLanguage(), countryCode));
return new FiatCurrency(currency.getCurrencyCode(), locale);
}

public static Map<String, FiatCurrency> getFiatCurrencyByCode() {
checkNotNull(fiatCurrencyByCode, "applyLocale need to be called before accessing getFiatCurrencyByCode");
return fiatCurrencyByCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.currency;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@EqualsAndHashCode
@ToString
@Getter
public abstract class MisqCurrency implements Comparable<MisqCurrency> {
protected final String code;
@EqualsAndHashCode.Exclude
protected final String name;

public static boolean isFiat(String code) {
return FiatCurrencyRepository.getFiatCurrencyByCode().containsKey(code);
}

// We only can check if the currency is not fiat and if the code matches the format, but we do not maintain a list
// of crypto currencies to be flexible with any newly added one.
public static boolean isMaybeCrypto(String code) {
chimp1984 marked this conversation as resolved.
Show resolved Hide resolved
return !isFiat(code) && code.length() >= 3;
}

public MisqCurrency(String code, String name) {
this.code = code;
this.name = name;
}

public String getDisplayPrefix() {
return "";
}

public String getNameAndCode() {
return name + " (" + code + ")";
}

public String getCodeAndName() {
return code + " (" + name + ")";
}

@Override
public int compareTo(MisqCurrency other) {
return this.name.compareTo(other.name);
}
}
23 changes: 23 additions & 0 deletions common/src/main/java/network/misq/common/data/Couple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.data;

import java.io.Serializable;

public record Couple<A, B>(A first, B second) implements Serializable {
}
21 changes: 21 additions & 0 deletions common/src/main/java/network/misq/common/data/Triple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package network.misq.common.data;

public record Triple<A, B, C>(A first, B second, C third) {
}
Loading