Use lazy singleton instead of eager

This commit is contained in:
Manfred Karrer 2015-03-09 13:56:28 +01:00
parent cabfb4eb3e
commit 4f6761a6d9
13 changed files with 90 additions and 50 deletions

View File

@ -36,6 +36,7 @@ import io.bitsquare.trade.tomp2p.TomP2PTradeMessageModule;
import io.bitsquare.user.User;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.File;
@ -56,17 +57,17 @@ class BitsquareAppModule extends BitsquareModule {
@Override
protected void configure() {
bind(User.class).asEagerSingleton();
bind(Preferences.class).asEagerSingleton();
bind(AccountSettings.class).asEagerSingleton();
bind(User.class).in(Singleton.class);
bind(Preferences.class).in(Singleton.class);
bind(AccountSettings.class).in(Singleton.class);
File persistenceDir = new File(env.getRequiredProperty(Persistence.DIR_KEY));
bind(File.class).annotatedWith(named(Persistence.DIR_KEY)).toInstance(persistenceDir);
bindConstant().annotatedWith(named(Persistence.PREFIX_KEY)).to(env.getRequiredProperty(Persistence.PREFIX_KEY));
bind(Persistence.class).asEagerSingleton();
bind(Persistence.class).in(Singleton.class);
bind(Environment.class).toInstance(env);
bind(UpdateProcess.class).asEagerSingleton();
bind(UpdateProcess.class).in(Singleton.class);
install(networkModule());
install(bitcoinModule());

View File

@ -20,6 +20,7 @@ package io.bitsquare.btc;
import io.bitsquare.BitsquareModule;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.File;
@ -38,19 +39,19 @@ public class BitcoinModule extends BitsquareModule {
protected void configure() {
bind(BitcoinNetwork.class).toInstance(
env.getProperty(BitcoinNetwork.KEY, BitcoinNetwork.class, BitcoinNetwork.DEFAULT));
bind(FeePolicy.class).asEagerSingleton();
bind(FeePolicy.class).in(Singleton.class);
bindConstant().annotatedWith(named(UserAgent.NAME_KEY)).to(env.getRequiredProperty(UserAgent.NAME_KEY));
bindConstant().annotatedWith(named(UserAgent.VERSION_KEY)).to(env.getRequiredProperty(UserAgent.VERSION_KEY));
bind(UserAgent.class).asEagerSingleton();
bind(UserAgent.class).in(Singleton.class);
File walletDir = new File(env.getRequiredProperty(WalletService.DIR_KEY));
bind(File.class).annotatedWith(named(WalletService.DIR_KEY)).toInstance(walletDir);
bindConstant().annotatedWith(named(WalletService.PREFIX_KEY)).to(
env.getRequiredProperty(WalletService.PREFIX_KEY));
bind(WalletService.class).asEagerSingleton();
bind(WalletService.class).in(Singleton.class);
bind(BlockChainService.class).asEagerSingleton();
bind(BlockChainService.class).in(Singleton.class);
}
@Override

View File

@ -19,6 +19,8 @@ package io.bitsquare.crypto;
import io.bitsquare.BitsquareModule;
import com.google.inject.Singleton;
import org.springframework.core.env.Environment;
public class CryptoModule extends BitsquareModule {
@ -29,7 +31,7 @@ public class CryptoModule extends BitsquareModule {
@Override
protected void configure() {
bind(SignatureService.class).asEagerSingleton();
bind(HashService.class).asEagerSingleton();
bind(SignatureService.class).in(Singleton.class);
bind(HashService.class).in(Singleton.class);
}
}

View File

@ -20,7 +20,7 @@ package io.bitsquare.gui;
import io.bitsquare.BitsquareModule;
import io.bitsquare.gui.components.Popups;
import io.bitsquare.gui.main.MainView;
import io.bitsquare.gui.main.trade.offerbook.OfferBook;
import io.bitsquare.offer.OfferBook;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.gui.util.Transitions;
import io.bitsquare.gui.util.validation.BankAccountNumberValidator;
@ -60,20 +60,20 @@ public class GuiModule extends BitsquareModule {
bind(ViewFactory.class).to(InjectorViewFactory.class);
bind(ResourceBundle.class).toInstance(BSResources.getResourceBundle());
bind(ViewLoader.class).to(FxmlViewLoader.class).asEagerSingleton();
bind(CachingViewLoader.class).asEagerSingleton();
bind(ViewLoader.class).to(FxmlViewLoader.class).in(Singleton.class);
bind(CachingViewLoader.class).in(Singleton.class);
bind(OfferBook.class).asEagerSingleton();
bind(Navigation.class).asEagerSingleton();
bind(OverlayManager.class).asEagerSingleton();
bind(BSFormatter.class).asEagerSingleton();
bind(OfferBook.class).in(Singleton.class);
bind(Navigation.class).in(Singleton.class);
bind(OverlayManager.class).in(Singleton.class);
bind(BSFormatter.class).in(Singleton.class);
bind(BankAccountNumberValidator.class).asEagerSingleton();
bind(BtcValidator.class).asEagerSingleton();
bind(FiatValidator.class).asEagerSingleton();
bind(InputValidator.class).asEagerSingleton();
bind(PasswordValidator.class).asEagerSingleton();
bind(Transitions.class).asEagerSingleton();
bind(BankAccountNumberValidator.class).in(Singleton.class);
bind(BtcValidator.class).in(Singleton.class);
bind(FiatValidator.class).in(Singleton.class);
bind(InputValidator.class).in(Singleton.class);
bind(PasswordValidator.class).in(Singleton.class);
bind(Transitions.class).in(Singleton.class);
bind(Stage.class).toInstance(primaryStage);
Popups.primaryStage = primaryStage;

View File

@ -23,6 +23,7 @@ import io.bitsquare.locale.Country;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.offer.Direction;
import io.bitsquare.offer.Offer;
import io.bitsquare.offer.OfferBook;
import io.bitsquare.settings.Preferences;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.User;
@ -35,8 +36,6 @@ import org.bitcoinj.utils.Fiat;
import com.google.inject.Inject;
import java.util.Comparator;
import viewfx.model.Activatable;
import viewfx.model.DataModel;
@ -77,7 +76,6 @@ class OfferBookDataModel implements Activatable, DataModel {
final StringProperty fiatCode = new SimpleStringProperty();
final StringProperty btcCode = new SimpleStringProperty();
final ObjectProperty<Country> bankAccountCountry = new SimpleObjectProperty<>();
final ObjectProperty<Comparator<OfferBookListItem>> comparator = new SimpleObjectProperty<>();
private Direction direction;

View File

@ -57,7 +57,7 @@ public class TomP2PNetworkModule extends NetworkModule {
)
);
bindConstant().annotatedWith(Names.named(NETWORK_INTERFACE_KEY)).to(env.getProperty(NETWORK_INTERFACE_KEY, NETWORK_INTERFACE_UNSPECIFIED));
bind(BootstrappedPeerBuilder.class).asEagerSingleton();
bind(BootstrappedPeerBuilder.class).in(Singleton.class);
}
@Override

View File

@ -15,13 +15,12 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui.main.trade.offerbook;
package io.bitsquare.offer;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.gui.main.trade.offerbook.OfferBookListItem;
import io.bitsquare.locale.Country;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.offer.Offer;
import io.bitsquare.offer.OfferBookService;
import io.bitsquare.trade.TradeManager;
import io.bitsquare.user.User;
import io.bitsquare.util.Utilities;
@ -93,10 +92,10 @@ public class OfferBook {
public void onOfferRemoved(Offer offer) {
// Update state in case that that offer is used in the take offer screen, so it gets updated correctly
offer.setState(Offer.State.OFFER_REMOVED);
// clean up possible references in tradeManager
tradeManager.handleRemovedOffer(offer);
offerBookListItems.removeIf(item -> item.getOffer().getId().equals(offer.getId()));
}
};
@ -107,13 +106,13 @@ public class OfferBook {
// Package scope
///////////////////////////////////////////////////////////////////////////////////////////
void addClient() {
public void addClient() {
numClients++;
if (numClients == 1)
startPolling();
}
void removeClient() {
public void removeClient() {
numClients--;
checkArgument(numClients >= 0);
if (numClients == 0)
@ -125,7 +124,7 @@ public class OfferBook {
// Getter
///////////////////////////////////////////////////////////////////////////////////////////
ObservableList<OfferBookListItem> getOfferBookListItems() {
public ObservableList<OfferBookListItem> getOfferBookListItems() {
return offerBookListItems;
}
@ -139,7 +138,9 @@ public class OfferBook {
if (bankAccount != null) {
country = bankAccount.getCountry();
fiatCode = bankAccount.getCurrency().getCurrencyCode();
offerBookListItems.stream().forEach(e -> e.setBankAccountCountry(country));
// TODO check why that was used
//offerBookListItems.stream().forEach(e -> e.setBankAccountCountry(country));
}
else {
fiatCode = CurrencyUtil.getDefaultCurrency().getCurrencyCode();

View File

@ -19,6 +19,8 @@ package io.bitsquare.offer;
import io.bitsquare.BitsquareModule;
import com.google.inject.Singleton;
import org.springframework.core.env.Environment;
public abstract class OfferModule extends BitsquareModule {
@ -27,4 +29,10 @@ public abstract class OfferModule extends BitsquareModule {
super(env);
}
@Override
protected void configure() {
bind(OfferBook.class).in(Singleton.class);
bind(OfferBook.class).in(Singleton.class);
}
}

View File

@ -18,10 +18,11 @@
package io.bitsquare.offer.tomp2p;
import io.bitsquare.network.tomp2p.TomP2PNode;
import io.bitsquare.offer.OfferModule;
import io.bitsquare.offer.OfferBookService;
import io.bitsquare.offer.OfferModule;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import javax.inject.Inject;
@ -37,7 +38,8 @@ public class TomP2POfferModule extends OfferModule {
@Override
protected void configure() {
bind(OfferBookService.class).toProvider(OfferBookServiceProvider.class).asEagerSingleton();
super.configure();
bind(OfferBookService.class).toProvider(OfferBookServiceProvider.class).in(Singleton.class);
}
}

View File

@ -21,7 +21,7 @@ import io.bitsquare.network.Message;
import io.bitsquare.network.MessageBroker;
import io.bitsquare.network.Peer;
import io.bitsquare.trade.listeners.GetPeerAddressListener;
import io.bitsquare.trade.listeners.HandleNewMessageListener;
import io.bitsquare.trade.listeners.ProcessNewMessageListener;
import io.bitsquare.trade.listeners.SendMessageListener;
import java.security.PublicKey;
@ -34,9 +34,9 @@ public interface TradeMessageService extends MessageBroker {
void sendMessage(Peer peer, Message message, SendMessageListener listener);
void addHandleNewMessageListener(HandleNewMessageListener listener);
void addHandleNewMessageListener(ProcessNewMessageListener listener);
void removeHandleNewMessageListener(HandleNewMessageListener listener);
void removeHandleNewMessageListener(ProcessNewMessageListener listener);
void getPeerAddress(PublicKey messagePublicKey, GetPeerAddressListener getPeerAddressListener);
}

View File

@ -19,6 +19,8 @@ package io.bitsquare.trade;
import io.bitsquare.BitsquareModule;
import com.google.inject.Singleton;
import org.springframework.core.env.Environment;
public class TradeModule extends BitsquareModule {
@ -29,6 +31,6 @@ public class TradeModule extends BitsquareModule {
@Override
protected void configure() {
bind(TradeManager.class).asEagerSingleton();
bind(TradeManager.class).in(Singleton.class);
}
}

View File

@ -0,0 +1,25 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare 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.
*
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.trade.listeners;
import io.bitsquare.network.Message;
import io.bitsquare.network.Peer;
public interface ProcessNewMessageListener {
void handleMessage(Message message, Peer sender);
}

View File

@ -23,7 +23,7 @@ import io.bitsquare.network.tomp2p.TomP2PNode;
import io.bitsquare.network.tomp2p.TomP2PPeer;
import io.bitsquare.trade.TradeMessageService;
import io.bitsquare.trade.listeners.GetPeerAddressListener;
import io.bitsquare.trade.listeners.HandleNewMessageListener;
import io.bitsquare.trade.listeners.ProcessNewMessageListener;
import io.bitsquare.trade.listeners.SendMessageListener;
import io.bitsquare.user.User;
@ -58,7 +58,7 @@ public class TomP2PTradeMessageService implements TradeMessageService {
private final TomP2PNode tomP2PNode;
private final User user;
private final List<HandleNewMessageListener> handleNewMessageListeners = new ArrayList<>();
private final List<ProcessNewMessageListener> processNewMessageListeners = new ArrayList<>();
private Executor executor;
@ -133,12 +133,12 @@ public class TomP2PTradeMessageService implements TradeMessageService {
// Event Listeners
///////////////////////////////////////////////////////////////////////////////////////////
public void addHandleNewMessageListener(HandleNewMessageListener listener) {
handleNewMessageListeners.add(listener);
public void addHandleNewMessageListener(ProcessNewMessageListener listener) {
processNewMessageListeners.add(listener);
}
public void removeHandleNewMessageListener(HandleNewMessageListener listener) {
handleNewMessageListeners.remove(listener);
public void removeHandleNewMessageListener(ProcessNewMessageListener listener) {
processNewMessageListeners.remove(listener);
}
@ -149,7 +149,7 @@ public class TomP2PTradeMessageService implements TradeMessageService {
@Override
public void handleMessage(Object message, Peer sender) {
if (message instanceof Message && sender instanceof TomP2PPeer) {
executor.execute(() -> handleNewMessageListeners.stream().forEach(e ->
executor.execute(() -> processNewMessageListeners.stream().forEach(e ->
e.handleMessage((Message) message, sender)));
}
}