001/******************************************************************************* 002 * Copyright (c) 2017 Red Hat Inc and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Red Hat Inc - initial API and implementation 011 *******************************************************************************/ 012package org.eclipse.kapua.gateway.client; 013 014import static java.util.Objects.requireNonNull; 015 016import java.util.Optional; 017 018/** 019 * An interface to publish data 020 * 021 * @param <X> base class of sender errors 022 */ 023public interface Sender<X extends Throwable> { 024 025 public void send(Payload payload) throws X; 026 027 public default void send(final Payload.Builder payload) throws X { 028 send(payload.build()); 029 } 030 031 public default <Y extends Throwable> Sender<Y> errors(final ErrorHandler<Y> errorHandler) { 032 return new Sender<Y>() { 033 034 @Override 035 public void send(final Payload payload) throws Y { 036 requireNonNull(payload); 037 038 try { 039 Sender.this.send(payload); 040 } catch (final Throwable e) { 041 errorHandler.handleError(e, Optional.of(payload)); 042 } 043 } 044 }; 045 046 } 047}